1

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

Json
  • 1,655
  • 1
  • 13
  • 21

2 Answers2

1

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).

Tom Udding
  • 2,264
  • 3
  • 20
  • 30
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.

Bman70
  • 743
  • 5
  • 11
  • You could put all the areas in an array with: `$areas = array(); foreach($data['items'] as $area) $areas[] = $area['forecasts'][0]['area'];` – Bman70 Jun 01 '17 at 20:02