Filter json data

I tried with this code but i get an error

$key = "***************";
$city = 'paris';
$url = "http://api.apixu.com/v1/forecast.json?key=$key&q=$city&days=7" ;

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

$json_output=curl_exec($ch);
$weather = json_decode($json_output);

foreach($weather->forecast->forecastday->hour as $forecastday)
{
    if($forecastday->time == "2016-09-01 01:00")
    {
        echo $forecastday->temp_c;
    }
}

PHP Notice: Trying to get property of non-object on line 13
PHP Warning: Invalid argument supplied for foreach() on line 13

the image of json tree https://s21.postimg.io/i46zynmp3/test.png

looks like your retrieved JSON is invalid.

use var_dump($wheather) to check the response itself and json_last_error() for the decoding success/failure.

var_dump gives me no error i think the problem is on the foreach, watch the image in the link and check did $forecastday path is correct

EDITED CODE
Retrive only data for the specified date

$key = "*****";

$city = 'paris';
$url = "http://api.apixu.com/v1/forecast.json?key=$key&q=$city&days=7" ;

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

$json_output=curl_exec($ch);
$weather = json_decode($json_output);
///echo "Temperature: " . $weather->forecast->forecastday[0]->hour[0]->time;

foreach($weather->forecast->forecastday[0]->hour as $hour) {
    if($hour->time = '2016-09-01 00:00') {
        echo $hour->temp_c," -- ";
    }
}

When i run the code i get all temp_c results for all hours of that date not the specified hour. How to get only the result for specified date ?

18.9 – 17.2 – 16.5 – 15.9 – 15.2 – 14.9 – 14.6 – 14.3 – 15.6 – 16.9 – 18.2 – 21.5 – 24.9 – 28.2 – 28.7 – 29.2 – 29.7 – 28.9 – 28 – 27.2 – 26.5 – 25.9 – 25.2 – 23.5 –
Process finished with exit code 0

the image of json tree https://s21.postimg.io/i46zynmp3/test.png

    if($hour->time = '2016-09-01 00:00') {

needs to be

    if($hour->time == '2016-09-01 00:00') {

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.