6

I have the following array

"forecast":{
    "txt_forecast": {
    "date":"11:00 PM EDT",
    "forecastday": [
    {
    "period":0,
    "icon":"partlycloudy",
    "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
    "title":"Saturday",
    "fcttext":"Partly cloudy in the morning, then mostly cloudy. High of 88F. Winds from the South at 5 to 10 mph.",
    "fcttext_metric":"Partly cloudy in the morning, then mostly cloudy. High of 31C. Winds from the South at 10 to 15 km/h.",
    "pop":"0"
    }
    ,
    {
    "period":1,
    "icon":"partlycloudy",
    "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
    "title":"Saturday Night",
    "fcttext":"Overcast in the evening, then partly cloudy. Low of 64F. Winds less than 5 mph.",
    "fcttext_metric":"Overcast in the evening, then partly cloudy. Low of 18C. Winds less than 5 km/h.",
    "pop":"10"
    }

How would I display the information in PHP? It goes all the way up to period 7.

EDIT: Here is my edited code that is not working.

<?php
$json_string =    file_get_contents("http://api.wunderground.com/api/7ec5f6510a4656df/geolookup/forecast/q/40121.json");
$parsed_json = json_decode($json_string);
$temp = $parsed_json->{'forecast'}->{'txt_forecast'}->{'date'};
$title = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}->{'title'};
$for = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}->{'fcttext'};
echo "Current date is ${temp},  ${title}: ${for}\n";

foreach($parsed_json['forecast']['forecastday[0]'] as $key => $value)
{
   echo $value['period'];
   echo $value['icon'];
   // etc
}
?>
4
  • Note that your JSON is invalid: you need to surround it in { }, or remove "forecast": . Plus, you never close your square brackets. Commented Jun 10, 2012 at 10:08
  • Looks like you want the alternate form of decoding: my answer Commented Jun 10, 2012 at 10:10
  • Note that $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}->{'fcttext'} is equivalent to $parsed_json->forecast->txt_forecast->forecastday->fcttext Commented Jun 10, 2012 at 10:10
  • I guess im missing what you mean on this.. Could you show me? Commented Jun 10, 2012 at 10:11

2 Answers 2

8

Update

$json_string =    file_get_contents("http://api.wunderground.com/api/7ec5f6510a4656df/geolookup/forecast/q/40121.json");
$parsed_json = json_decode($json_string, true);
$parsed_json = $parsed_json['forecast']['txt_forecast']['forecastday'];
//pr($parsed_json);

foreach($parsed_json as $key => $value)
{
   echo $value['period'] . '<br>';
   echo $value['icon'] . '<br>';
   // etc
}

Result:

0
chancetstorms
1
tstorms
2
tstorms
3
tstorms
4
tstorms
5
partlycloudy
6
partlycloudy
7
partlycloudy

You will have to use json_decode() function for that.

$myArray = json_decode($yourJSON, true);

Now you can use foreach to get the data from $myArray array eg:

foreach($myArray['forecast']['forecastday'] as $key => $value)
{
   echo $value['period'];
   echo $value['icon'];
   // etc
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Eric: To convert it to an array and that's my personal preference :)
@DavidMorin: See updated answer plz. You did not provide correct json start point in your question first :)
2

You can also convert json to PHP objects:

$myData = json_decode($yourJSON);

foreach($myData->forecast->forecastday as $day) {
   echo $day->period;
   echo $day->icon;
}

EDIT:

I think you can fix the code in your question with this:

<?php
$json_string =    file_get_contents("http://api.wunderground.com/api/7ec5f6510a4656df/geolookup/forecast/q/40121.json");
$parsed_json = json_decode($json_string);
$forecast = $parsed_json->forecast->txt_forecast;
$temp = $forecast->date;
$title = $forecast->forecastday->title;
$for = $forecast->forecastday->fcttext;
echo "Current date is ${temp},  ${title}: ${for}\n";

foreach($forecast->forecastday as $value) {
   echo $value['period'];
   echo $value['icon'];
   // etc
}
?>

8 Comments

Warning: Invalid argument supplied for foreach()
@DavidMorin: What does print_r($myData->forecast->forecastday) give?
@DavidMorin: What about print_r($myData->forecast)?
with your above edit i get Parse error: syntax error, unexpected T_VARIABLE in
@DavidMorin: Missing semicolon
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.