用PHP中的关联数组显示键/值中的值



我在这里处理这个json,http://api.wunderground.com/api/72cc0e0d32f5f1ea/history_20150303/q/90210.json

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

这是我的密码。它只显示关键点,而不显示值。例如"雾":"0","雨":"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>";
    }

最新更新