2

I know how to decode a JSON string and get the data from one dimensional array but how to get the data from the nested array? Below is my code:

$data = json_decode($json);

and below is the JSON return value:

{
      "area_metadata": [
        {
          "name": "A",
          "label_location": {
            "latitude": 1,
            "longitude": 1
          }
        },
        {
          "name": "B",
          "label_location": {
            "latitude": 1,
            "longitude": 1
          }
        }   
      ],
     "items": [
            {
              "update_timestamp": "2017-05-02T09:51:20+08:00",
              "timestamp": "2017-05-02T09:31:00+08:00",
              },
              "locations": [
                {
                  "area": "A",
                  "weather": "Showers"
                },
                {
                  "area": "B",
                  "weather": "Cloudy"
                }
              ]
            }
        ]}

I had tested:

echo $data->items->locations[0]->area;

but I got this error

Trying to get property of non-object

Also,I tried to convert JSON into array instead of object:

$data = json_decode($json,true);


if (isset($data)) 
{
    foreach ($data->items->locations as $location) 
    {
            if (empty($location["area"])) { continue; }
            if ($location["area"] == "A") 
            {
                echo $location["weather"];

            }


    }
}

but it also not working.

Could anyone can advise which step that I did wrongly? Thanks!

Edited: Below is the pastebin link with full JSON content. https://pastebin.com/cewszSZD

14
  • It appears that your JSON is malformed, are you sure this is the exact JSON string you are trying to decode? The decode might "fail" due to the malformed JSON. Commented Jun 1, 2017 at 17:16
  • Not responding to my question does not help solving your problem. I understand it if you cannot be here all the time, but people appreciate it if you respond a little sooner. Commented Jun 1, 2017 at 17:25
  • I missed out a "}", just added it back to the JSON Data.Thanks. Commented Jun 1, 2017 at 18:08
  • I didn't paste the whole JSON data here,cos it was very long,but the overall structure is similar. Commented Jun 1, 2017 at 18:13
  • 1
    @TomUdding Thanks for your prompt reply.I live in Asia time zone and it is midnight now,I will try my best to reply asap. Commented Jun 1, 2017 at 18:18

2 Answers 2

2

The JSON you provided (in your question) is malformed and using json_decode() on it will result in NULL. Thus nothing will happen when you try to access the decoded object because it doesn't exist.

The full JSON you provided is valid and the reason why your code didn't yield any results is because in items there is an "inner"-array:

(...) 
["items"] => array(1) {
    [0] => array(4) {
//  ^^^^^^^^^^^^^^^^^
        ["update_timestamp"] => string(25) "2017-05-02T09:21:18+08:00" 
        ["timestamp"] => string(25) "2017-05-02T09:07:00+08:00" 
        ["valid_period"] => array(2) { 
            ["start"] => string(25) "2017-05-02T09:00:00+08:00" 
            ["end"] => string(25) "2017-05-02T11:00:00+08:00" 
        } 
        ["forecasts"] => array(47) { 
            [0] => array(2) { 
                ["area"] => string(10) "Ang Mo Kio" 
                ["forecast"] => string(19) "Partly Cloudy (Day)" 
            }
            (...)

You'll have to access that array through key 0, for arrays it will look like this:

$data = json_decode($json, true);
echo $data['items'][0]['forecasts'][0]['area'];
//                 ^^^

And for objects like this:

$data = json_decode($json);
echo $data->items[0]->forecasts[0]->area;
//               ^^^

The second 0 changes the location (the different arrays in the forecasts array).

You can check the output here (array approach) and here (object approach).

Sign up to request clarification or add additional context in comments.

Comments

0

It would be easier to help if you post all the JSON data or link to a screenshot of it. Try:

$items[0]['locations'][0]['area'];

Single quotes on strings, no quotes on numbers.

1 Comment

You could put all the areas in an array with: $areas = array(); foreach($data['items'] as $area) $areas[] = $area['forecasts'][0]['area'];

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.