lStoilov
lStoilov

Reputation: 1339

Take values from JSON with PHP

I am doing something wrong but I cannot figure it out.

I have this JSON

$data = {  
   "data":[  
      {  
         "app_min_temp":15.5,
         "valid_date":"2018-08-10",
         "weather":{  
            "icon":"c02d",
            "code":801,
            "description":"Few clouds"
         },
         "max_temp":26.3,
         "datetime":"2018-08-11"
      },
      {  
         "app_min_temp":18.5,
         "valid_date":"2018-08-11",
         "weather":{  
            "icon":"c02d",
            "code":801,
            "description":"Few clouds"
         },
         "max_temp":26.3,
         "datetime":"2018-08-11"
      }
   ],
   "city_name":"Berlin"
};

Expected Result And trying to get the values from app_min_temp and valid_date and print them all together. Something like

app_min_temp:15.5 valid_date: 2018-08-10 Weather description: Few clouds
app_min_temp:18.5 valid_date: 2018-08-11 Weather description: Few clouds

FOREACH I am trying to foreach trougn the json like this

$json = json_decode($data);
foreach($json as $data) {
        echo "app_min_temp: ".$data[0]->app_min_temp.PHP_EOL."valid_date: ".$data[0]->valid_date.PHP_EOL."Weather description: ".$data[0]->weather->description.PHP_EOL; //etc
    }

The Results But I am getting strange result with only the first set of data printed. Like:

app_min_temp: 15.5
valid_date: 2018-08-10
Weather description: Few clouds

app_min_temp: 
valid_date: 
Weather description: 
app_min_temp: 
valid_date: 
Weather description: 
app_min_temp: 
valid_date: 
Weather description: 
app_min_temp: 
valid_date: 
Weather description: 
app_min_temp: 
valid_date: 
Weather description: 
app_min_temp: 
valid_date: 
Weather description: 

Thanks

Upvotes: 0

Views: 85

Answers (3)

Anas Bin Numan
Anas Bin Numan

Reputation: 64

You missed the nesting in JSON data. Your $data and json's data are totally different. I changed your loop section a bit . And the output was as you expected.

 <?php
$data = '{  
  "data":[  
   {  
     "app_min_temp":15.5,
     "valid_date":"2018-08-10",
     "weather":{  
        "icon":"c02d",
        "code":801,
        "description":"Few clouds"
     },
     "max_temp":26.3,
     "datetime":"2018-08-11"
  },
  {  
     "app_min_temp":18.5,
     "valid_date":"2018-08-11",
     "weather":{  
        "icon":"c02d",
        "code":801,
        "description":"Few clouds"
     },
     "max_temp":26.3,
     "datetime":"2018-08-11"
  }
],
"city_name":"Berlin"
}';

$json = json_decode($data);

foreach($json->data as $data) {
    echo "app_min_temp: ".$data->app_min_temp.PHP_EOL."valid_date: ".$data-   >valid_date.PHP_EOL."Weather description: ".$data->weather->description."<br>"; 
}
?>

Output

app_min_temp: 15.5 valid_date: 2018-08-10 Weather description: Few clouds
app_min_temp: 18.5 valid_date: 2018-08-11 Weather description: Few clouds

Upvotes: 1

K F
K F

Reputation: 1546

Another solution based on this post where it says that "if you want an associative array instead of an object from json_decode" you should do this way:

$json = json_decode($data,true);  // CHANGE

foreach($json['data'] as $data2) {
        echo "app_min_temp: ".$data2['app_min_temp'].PHP_EOL.
            "valid_date: ".$data2['valid_date'].PHP_EOL.
            "Weather description: ".$data2['weather']['description'].PHP_EOL; //etc
}

The true flag means returned objects will be converted into associative arrays.

Upvotes: 1

Tobias K.
Tobias K.

Reputation: 3092

There is a data key in your JSON object you need to access first. Also, the array index access is done by foreach already.

foreach ($json->data as $row) {
   echo "app_min_temp: ".$row->app_min_temp;

Upvotes: 2

Related Questions