Question

I have got this problem: I am trying to display weather info from Weatherground and I' m receiving this response:

{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"forecast": 1
}
  }
    ,
  "forecast":{
    "txt_forecast": {
    "date":"10:52 CEST",
    "forecastday": [
    {
    "period":0,
    "icon":"tstorms",
    "icon_url":"http://icons.wxug.com/i/c/k/tstorms.gif",
    "title":"Venerdì",
    "fcttext":"Temporali. Max: 79º F. Venti da S variabili. Prob. pioggia: 80%.",
    "fcttext_metric":"Temporali. Max: 26º C. Venti da S variabili. Prob. pioggia: 80%.",
    "pop":"80"
    }
    ,
    {
    "period":1,
    "icon":"nt_rain",
    "icon_url":"http://icons.wxug.com/i/c/k/nt_rain.gif",
    "title":"Venerdì notte",
    "fcttext":"Pioggia e tuoni. Min: 62º F. Venti da ENE variabili. Prob. pioggia: 60%.",
    "fcttext_metric":"Pioggia e tuoni. Min: 17º C. Venti da ENE variabili. Prob. pioggia: 60%.",
    "pop":"60"
    }
    ,
    {
    "period":2,
    "icon":"tstorms",
    "icon_url":"http://icons.wxug.com/i/c/k/tstorms.gif",
    "title":"Sabato",
    "fcttext":"Temporali. Max: 75º F. Venti da SSE variabili. Prob. pioggia: 90%.",
    "fcttext_metric":"Temporali. Max: 24º C. Venti da SSE variabili. Prob. pioggia: 90%.",
    "pop":"90"
    }

Using this code:

    <?php
    $json_string =   file_get_contents("http://api.wunderground.com/api/***/forecast/lang:IT/q/SW/Lugano.json");
     $parsed_json = json_decode($json_string);
     $date = $parsed_json->{'forecast'}->{'txt_forecast'}->{'date'};
     $named0 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}->{'title'};
     $txtd0 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}->{'fcttext'};

echo "$named0, $date: $txtd0";
 ?>

(I would like to get something like: Friday, 10:52CEST: Rainy day...)

But the problem is that I cannot display a forecast because the values for the second period ( period:1) have the same names of the first one.

I tried to put the link in http://jsonformatter.curiousconcept.com/ but it just shows me this: ...

"forecastday":[  
            {  
               "period":0,
               "icon":"tstorms",
               "icon_url":"http://icons.wxug.com/i/c/k/tstorms.gif",
               "title":"Venerdì",
               "fcttext":"Temporali. Max: 79º F. Venti da S variabili. Prob. pioggia: 80%.",
               "fcttext_metric":"Temporali. Max: 26º C. Venti da S variabili. Prob. pioggia: 80%.",
               "pop":"80"
            },
            {  },
            {  },
            {  },
            {  },
            {  },
            {  },
            {  }

So the question is, how can I separate the different period like this:

period0: $named0, $date: $txtd0

period1: $named1, $date: $txtd1
....

Because in the code i see no way to get the first or 2nd 'title' of the response

$named0 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}->{'title'};

$named0 must get the value of the first 'title'

Thanky You and sorry for my english and my coding skills, I am still a newbie

Was it helpful?

Solution

'forecastday' is decoded as array, so ypou can access it by:

$name1 = $parsed_json->forecast->forecastday[1]->name;

I your notation (have not used/tried that) it might be:

$name1 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[1]->{'title'};

(The brakets do not go into the string / the '...') However, forecastday is an array, the others are Objects...

OTHER TIPS

Like this:

  $data = json_decode('{"response": {"version":"0.1","termsofService":"http://www.wunderground.co/weather/api/d/terms.html","features": {"forecast": 1}},"forecast":{"txt_forecast": {"date":"10:52 CEST", "forecastday": [{"period":0,"icon":"tstorms","icon_url":"http://icons.wxug.com/i/c/k/tstorms.gif", "title":"Venerdì", "fcttext":"Temporali. Max: 79º F. Venti da S variabili. Prob. pioggia: 80%.","fcttext_metric":"Temporali. Max: 26º C. Venti da S variabili. Prob. pioggia: 80%.","pop":"80"},{"period":1,"icon":"nt_rain","icon_url":"http://icons.wxug.com/i/c/k/nt_rain.gif","title":"Venerdì notte","fcttext":"Pioggia e tuoni. Min: 62º F. Venti da ENE variabili. Prob. pioggia: 60%.","fcttext_metric":"Pioggia e tuoni. Min: 17º C. Venti da ENE variabili. Prob. pioggia: 60%.","pop":"60"},{"period":2,"icon":"tstorms","icon_url":"http://icons.wxug.com/i/c/k/tstorms.gif","title":"Sabato","fcttext":"Temporali. Max: 75º F. Venti da SSE variabili. Prob. pioggia: 90%.","fcttext_metric":"Temporali. Max: 24º C. Venti da SSE variabili. Prob. pioggia: 90%.","pop":"90"}]}}}');


  foreach ($data->forecast->txt_forecast->forecastday as $forecast)
  {
      echo printf("%s, %s, %s<br/>", $forecast->title, $data->forecast->txt_forecast->date, $forecast->fcttext);
  }

Even though you've json_decode as an object, you can loop forecastday the same way you would for an array.

foreach( $parsed_json->response->forecast->txt_forecast->forecastday as $key=> $day )
{
    print( 'Period' . $day->period . ': ' );
    print( $day->title . ', ' );
    print( $parsed_json->response->forecast->txt_forecast->date . ' ' );
    print( $day->fcttext );
}

or you can access it directly like this:

$forecastzerotitle = $parsed_json->response->forecast->txt_forecast->forecastday[0]->title );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top