Mark Henry
Mark Henry

Reputation: 2709

Get value in json string

I have this json result for which I would like to get the tempMaxC of a specific date (e.g. 2013-10-14).

I have tried this but it is not working.

$json=json_decode($json_reply);


printf("<p>Current temp</p>", 

    $json->{'data'}->{'weather'}['0']->{'date'}, 
    $json->{'data'}->{'weather'}['0']->{'tempMaxC'} );

Json:

    {
    "data": {
        "current_condition": [
            {
                "cloudcover": "25",
                "humidity": "81",
                "observation_time": "08:51 AM",
                "precipMM": "0.0",
                "pressure": "1019",
                "temp_C": "6",
                "temp_F": "43",
                "visibility": "10",
                "weatherCode": "113",
                "weatherDesc": [
                    {
                        "value": "Sunny"
                    }
                ],
                "weatherIconUrl": [
                    {
                        "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
                    }
                ],
                "winddir16Point": "SSW",
                "winddirDegree": "210",
                "windspeedKmph": "9",
                "windspeedMiles": "6"
            }
        ],
        "request": [
            {
                "query": "Adelboden, Switzerland",
                "type": "City"
            }
        ],
        "weather": [
            {
                "date": "2013-10-13",
                "precipMM": "3.2",
                "tempMaxC": "7",
                "tempMaxF": "44",
                "tempMinC": "0",
                "tempMinF": "32",
                "weatherCode": "116",
                "weatherDesc": [
                    {
                        "value": "Partly Cloudy"
                    }
                ],
                "weatherIconUrl": [
                    {
                        "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png"
                    }
                ],
                "winddir16Point": "SW",
                "winddirDegree": "233",
                "winddirection": "SW",
                "windspeedKmph": "12",
                "windspeedMiles": "7"
            },
            {
                "date": "2013-10-14",
                "precipMM": "0.6",
                "tempMaxC": "9",
                "tempMaxF": "48",
                "tempMinC": "0",
                "tempMinF": "32",
                "weatherCode": "116",
                "weatherDesc": [
                    {
                        "value": "Partly Cloudy"
                    }
                ],
                "weatherIconUrl": [
                    {
                        "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png"
                    }
                ],
                "winddir16Point": "SSW",
                "winddirDegree": "213",
                "winddirection": "SSW",
                "windspeedKmph": "12",
                "windspeedMiles": "8"
            }
        ]
    }
}

Upvotes: 0

Views: 203

Answers (3)

user2092317
user2092317

Reputation: 3348

try this

$json=json_decode($json_reply);

    foreach($json->data->weather as $c ){


     if($c->date == '2013-10-14'){

       printf("<p>Date: %s | Temp: %s°C</p>", $c->date, $c->tempMaxC );

     }


    }

Upvotes: 0

Glavić
Glavić

Reputation: 43582

You need to tell function printf() how arguments should be printed in format:

printf("<p>Date: %s | Temp: %s°C</p>", $arg1, $arg2);

Loop over days:

foreach ($json->data->weather as $day) {
    printf("<p>Date: %s | Temp: %s°C</p>", $day->date, $day->tempMaxC );
}

Demo.

Upvotes: 2

Tamil Selvan C
Tamil Selvan C

Reputation: 20239

You forgot to specify the format in printf function. ie %d, %s ....

Try

printf("<p>Current temp %s %s</p>", $json->{'data'}->{'weather'}['0']->{'date'}, $json->{'data'}->{'weather'}['0']->{'tempMaxC'} );

Refer: http://php.net/manual/en/function.printf.php for printf syntax

Loop over days:

foreach( $json->{'data'}->{'weather'} as $days )
{
    printf("<p>Current temp %s %s</p>", 
    $days->{'date'}, 
    $days->{'tempMaxC'} );
}

Upvotes: 0

Related Questions