I have a string of data that looks like this:
"forecast":{
"txt_forecast": {
"date":"1:48 PM BST",
"forecastday": [
{
"period":0,
"icon":"partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
"title":"Wednesday",
"fcttext":"Sunshine and clouds mixed. High 68F. Winds NNE at 5 to 10 mph.",
"fcttext_metric":"Sunshine and clouds mixed. High 20C. Winds NNE at 10 to 15 kph.",
"pop":"20"
}
,
{
"period":1,
"icon":"nt_clear",
"icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif",
"title":"Wednesday Night",
"fcttext":"A few passing clouds. Low 52F. Winds NE at 5 to 10 mph.",
"fcttext_metric":"A few passing clouds. Low 11C. Winds NE at 10 to 15 kph.",
"pop":"10"
}
,
{
"period":2,
"icon":"partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
"title":"Thursday",
"fcttext":"Partly cloudy skies. High 69F. Winds N at 10 to 15 mph.",
"fcttext_metric":"Partly cloudy. High 20C. Winds N at 15 to 25 kph.",
"pop":"20"
}
,
{
"period":3,
"icon":"nt_partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/nt_partlycloudy.gif",
"title":"Thursday Night",
"fcttext":"Partly cloudy skies. Low 51F. Winds NE at 10 to 15 mph.",
"fcttext_metric":"A few clouds from time to time. Low around 10C. Winds NE at 10 to 15 kph.",
"pop":"0"
}
Im trying to parse this into PHP so that I can display the weather for the next 3 days:
$parsed_json = json_decode($json_string);
// Get the forecasts for the next 3 days
$todaysWeather = $parsed_json->{'forecast'}->{'period'=0}->{'fcttext_metric'}
$tomorrowsWeather = $parsed_json->{'forecast'}->{'period'=2}->{'fcttext_metric'}
$dayThreeWeather = $parsed_json->{'forecast'}->{'period'=4}->{'fcttext_metric'}
echo "Todays Weather is: " . $todaysWeather;
echo "Tomorrows Weather is: " . $tomorrowsWeather;
echo "3rd Day Weather is: " . $dayTheeWeather;
I know that the {'period'=0} is wrong but I don't know the correct code to fix it. I've tried:
{'period'=0} {'period=0'}
But to no avail. Am I going about this the right way? Thanks Mathew