0

I have a PHP page where I reference a JSON object that looks like this:

{
  "body": {
    "zip": "02110",
    "stores": [
      {
        "storeEmail": "[email protected]",
        "storeName": "Name",
        "city": "City",
        "Availability": {
          "123": {
            "Quote": "daily",
            "Display": "available",
          }
        },
      },

Each JSON object contains multiple "stores", above is one example.

I can currently echo the store name by using this:

echo "<br>".$phpArray->body->stores{0}->storeName;

How do I echo the value "123" from the sample JSON? I would also like to echo the quote as a separate variable. The value "123" will change for different searches. Any help would be greatly appreciated!

2 Answers 2

1
$phpArray = json_decode($json, true);
foreach($phpArray['body']['stores'] as $store) {
    echo $store['storeName'];
    foreach{$store['Availabilty'] as $avail => $info) { 
        echo $avail; // 123
        echo $info['Quote'];
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hmm.. I am getting the error: Parse error: syntax error, unexpected 'echo' (T_ECHO) pointing to the line: echo $store['storeName'];
Whoops, missed a parenthesis.
1
$b = json_decode($a); 
var_dump(key($b->body->stores{0}->Availability)); 
var_dump(reset($b->body->stores{0}->Availability)->Quote); 

or a loop for stores

foreach($b->body->stores as $store) {
    var_dump(key($store->Availability)); 
    var_dump(reset($store->Availability)->Quote);
}

2 Comments

Using the first approach, I get an error: Warning: key() expects parameter 1 to be array, null given pointing to the first var_dump line
I use '{"body":{"zip":"02110","stores":[{"storeEmail":"[email protected]","storeName":"Name","city":"City","Availability":{"123":{"Quote":"daily","Display":"available"}}}]}}' for $a

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.