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

时间:2015-03-28 22:32:16

标签: php arrays json associative-array key-value

我在这里使用这个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":"" }
    ]

2 个答案:

答案 0 :(得分:0)

这应该适合你:

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
        //...

答案 1 :(得分:0)

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>";
    }