Json Php在日期字段中读取文本

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

The correct code to use is:

$parsed_json = json_decode($json_string);

// Get the forecasts for the next 3 days
$todaysWeather    = $parsed_json->forecast->txt_forecast->forecastday[0]->fcttext_metric;
$tomorrowsWeather = $parsed_json->forecast->txt_forecast->forecastday[2]->fcttext_metric;
$dayThreeWeather  = $parsed_json->forecast->txt_forecast->forecastday[4]->fcttext_metric;

echo "Todays Weather is: " . $todaysWeather;
echo "Tomorrows Weather is: " . $tomorrowsWeather;
echo "3rd Day Weather is: " . $dayTheeWeather;

The following is the variable structure of the object $parsed_json

[forecast] => stdClass Object
    (
        [txt_forecast] => stdClass Object
            (
                [date] => 1:48 PM BST
                [forecastday] => Array
                    (
                        [0] => stdClass Object
                            (
                                [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
                            )

                        [1] => stdClass Object
                            (
                                [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
                            )

                        [2] => stdClass Object
                            (
                                [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
                            )

                        [3] => stdClass Object
                            (
                                [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
                            )

                    )

            )

    )

Assuming you fix your JSON syntax, you're close -- you need to use array notation to get to the periods inside the forecastday section:

$data = json_decode($json);
$first_period = $data->forecast->txt_forecast->forecastday[0]->fcttext_metric;
$second_period = $data->forecast->txt_forecast->forecastday[1]->fcttext_metric;

Unfortunately php does not support such logic. Therefore you have to reformat your input a little bit.

$days = array();
foreach ($parsed_json->forecast->forecastday as $day) {
    $days[$day->period] = $day;
}
$todaysWeather    = $days[0]->fcttext_metric;
$tomorrowsWeather = $days[2]->fcttext_metric;
$dayThreeWeather  = $days[4]->fcttext_metric;

That being said, in your case this would work:

$todaysWeather = $parsed_json->forecast->forecastday[0]->fcttext_metric;

But that can be coincidence.