使用 PHP 中的关联数组显示 key/value 中的值

Displaying value from key/value with associative array in PHP

我正在使用此 json,http://api.wunderground.com/api/72cc0e0d32f5f1ea/history_20150303/q/90210.json

我正在尝试从 json 底部的 dailysummary 数组中检索 key/value 对。

这是我的代码。它只显示键而不显示值。例如"fog":"0","rain":"1"

谁能指出我哪里错了?非常感谢!

<?php

$json_string = file_get_contents("http://api.wunderground.com/api/72cc0e0d32f5f1ea/history_20150303/q/11374.json");
$parsed_json = json_decode($json_string);

   foreach ($parsed_json->{'history'}->{'dailysummary'}[0] as $key => $val){

      echo $key . ": " . $val . "<br>";
    }

    ?>

这是json

的相关部分
    "dailysummary": [
    { "date": {
    "pretty": "12:00 PM PST on March 03, 2015",
    "year": "2015",
    "mon": "03",
    "mday": "03",
    "hour": "12",
    "min": "00",
    "tzname": "America/Los_Angeles"
    },
    "fog":"0","rain":"1","snow":"0","snowfallm":"0.00", "snowfalli":"0.00","monthtodatesnowfallm":"", "monthtodatesnowfalli":"","since1julsnowfallm":"", "since1julsnowfalli":"","snowdepthm":"", "snowdepthi":"","hail":"0","thunder":"0","tornado":"0","meantempm":"13", "meantempi":"56","meandewptm":"6", "meandewpti":"42","meanpressurem":"1013", "meanpressurei":"29.93","meanwindspdm":"8", "meanwindspdi":"5","meanwdire":"","meanwdird":"281","meanvism":"16", "meanvisi":"10","humidity":"","maxtempm":"17", "maxtempi":"63","mintempm":"9", "mintempi":"48","maxhumidity":"83","minhumidity":"38","maxdewptm":"8", "maxdewpti":"47","mindewptm":"3", "mindewpti":"37","maxpressurem":"1015", "maxpressurei":"29.98","minpressurem":"1012", "minpressurei":"29.88","maxwspdm":"24", "maxwspdi":"15","minwspdm":"0", "minwspdi":"0","maxvism":"16", "maxvisi":"10","minvism":"16", "minvisi":"10","gdegreedays":"6","heatingdegreedays":"10","coolingdegreedays":"0","precipm":"0.25", "precipi":"0.01","precipsource":"","heatingdegreedaysnormal":"","monthtodateheatingdegreedays":"","monthtodateheatingdegreedaysnormal":"","since1sepheatingdegreedays":"","since1sepheatingdegreedaysnormal":"","since1julheatingdegreedays":"","since1julheatingdegreedaysnormal":"","coolingdegreedaysnormal":"","monthtodatecoolingdegreedays":"","monthtodatecoolingdegreedaysnormal":"","since1sepcoolingdegreedays":"","since1sepcoolingdegreedaysnormal":"","since1jancoolingdegreedays":"","since1jancoolingdegreedaysnormal":"" }
    ]

这应该适合你:

if(!is_object($val))
    echo $key . ": " . $val . "<br>";

这里我检查值是否不是对象,因为数组中还有一个 stdClass 对象,如果不添加此 if 语句,它会通过你报错。

您可以在下面的结构中看到该对象:

[dailysummary] => Array (
    [0] => stdClass Object (

        [date] => stdClass Object (
                //^^^^^^^^^^^^^^^ Here I check that the value is not an object
            [pretty] => 12:00 PM EST on March 03, 2015
            [year] => 2015
            [mon] => 03
            [mday] => 03
            [hour] => 12
            [min] => 00
            [tzname] => America/New_York
        )

        [fog] => 1
        [rain] => 1
        [snow] => 1
        [snowfallm] => 2.54
        [snowfalli] => 1.00
        //...

json_decode( $string, 1 ) 期间使用数组标志更容易循环:

$json_string = file_get_contents( 'in.json' );
$parsed_json = json_decode($json_string, 1 );
   foreach ( $parsed_json[ 'dailysummary' ][ 0 ] as $key => $val){

        var_dump( $parsed_json[ 'dailysummary' ][ 0 ][ $key ] );
        var_dump( $key );
        var_dump( $val );
      // echo $key . ": " . $parsed_json->dailysummary[ 0 ][ $val ] . "<br>";
    }