使用php从关联数组中仅提取某些值

时间:2017-02-21 13:18:44

标签: php arrays json foreach

我有一个关联数组,我只想提取from,hour,min和tempm中的三个值。这些是在数组的不同级别,我只是无法弄清楚如何提取这些项目的每一个出现。我已经能够使用json_decode和foreach回显每个值,但现在无法过滤。我看过array_map,array_filter和for循环,但是我很难过。

阵列的一部分在下面。

"history":{
   "date":{
      "pretty":"February 18, 2016",
      "year":"2016",
      "mon":"02",
      "mday":"18",
      "hour":"00",
      "min":"00",
      "tzname":"Europe/London"
   },
   "utcdate":{
      "pretty":"February 18, 2016",
      "year":"2016",
      "mon":"02",
      "mday":"18",
      "hour":"00",
      "min":"00",
      "tzname":"UTC"
   },
   "observations":[
      {
         "date":{
            "pretty":"12:00 AM GMT on February 18, 2016",
            "year":"2016",
            "mon":"02",
            "mday":"18",
            "hour":"00",
            "min":"00",
            "tzname":"Europe/London"
         },
         "utcdate":{
            "pretty":"12:00 AM GMT on February 18, 2016",
            "year":"2016",
            "mon":"02",
            "mday":"18",
            "hour":"00",
            "min":"00",
            "tzname":"UTC"
         },
         "tempm":"4",
         "tempi":"38",
         "dewptm":"2",
         "dewpti":"36",
         "hum":"91",
         "wspdm":"7.4",
         "wspdi":"4.6",
         "wgustm":"",
         "wgusti":"",
         "wdird":"300",
         "wdire":"WNW",
         "vism":"13",
         "visi":"8",
         "pressurem":"1010",
         "pressurei":"29.84",
         "windchillm":"-999",
         "windchilli":"-999",
         "heatindexm":"-9999",
         "heatindexi":"-9999",
         "precipm":"",
         "precipi":"",
         "conds":"Light Rain",
         "icon":"rain",
         "fog":"0",
         "rain":"1",
         "snow":"0",
         "hail":"0",
         "thunder":"0",
         "tornado":"0",
         "metar":"AAXX 18004 03649 11463 83004 10035 20024 30003 40105 53008 60031 76062 8671/92350"
      },
      {
         "date":{
            "pretty":"12:50 AM GMT on February 18, 2016",
            "year":"2016",
            "mon":"02",
            "mday":"18",
            "hour":"00",
            "min":"50",
            "tzname":"Europe/London"
         },
         "utcdate":{
            "pretty":"12:50 AM GMT on February 18, 2016",
            "year":"2016",
            "mon":"02",
            "mday":"18",
            "hour":"00",
            "min":"50",
            "tzname":"UTC"
         },
         "tempm":"3",
         "tempi":"38",
         "dewptm":"2",
         "dewpti":"36",
         "hum":"88",
         "wspdm":"9.3",
         "wspdi":"5.8",
         "wgustm":"",
         "wgusti":"",
         "wdird":"300",
         "wdire":"WNW",
         "vism":"8",
         "visi":"5",
         "pressurem":"1011",
         "pressurei":"29.86",
         "windchillm":"-999",
         "windchilli":"-999",
         "heatindexm":"-9999",
         "heatindexi":"-9999",
         "precipm":"",
         "precipi":"",
         "conds":"Light Rain",
         "icon":"rain",
         "fog":"0",
         "rain":"1",
         "snow":"0",
         "hail":"0",
         "thunder":"0",
         "tornado":"0",
         "metar":"AAXX 18014 03649 41458 83005 10032 20021 30007 40109 52009 72162 8161/ 90050 333 55300 2//// 81715 88460"
      },
      {
         "date":{
            "pretty":"12:50 AM GMT on February 18, 2016",
            "year":"2016",
            "mon":"02",
            "mday":"18",
            "hour":"00",
            "min":"50",
            "tzname":"Europe/London"
         },
         "utcdate":{
            "pretty":"12:50 AM GMT on February 18, 2016",
            "year":"2016",
            "mon":"02",
            "mday":"18",
            "hour":"00",
            "min":"50",
            "tzname":"UTC"
         },
         "tempm":"3.0",
         "tempi":"37.4",
         "dewptm":"2.0",
         "dewpti":"35.6",
         "hum":"93",
         "wspdm":"9.3",
         "wspdi":"5.8",
         "wgustm":"-9999.0",
         "wgusti":"-9999.0",
         "wdird":"300",
         "wdire":"WNW",
         "vism":"8.0",
         "visi":"5.0",
         "pressurem":"1010",
         "pressurei":"29.83",
         "windchillm":"0.5",
         "windchilli":"32.8",
         "heatindexm":"-9999",
         "heatindexi":"-9999",
         "precipm":"-9999.00",
         "precipi":"-9999.00",
         "conds":"Haze",
         "icon":"hazy",
         "fog":"0",
         "rain":"0",
         "snow":"0",
         "hail":"0",
         "thunder":"0",
         "tornado":"0",
         "metar":"METAR EGVN 180050Z 30005KT 8000 HZ FEW015 OVC100 03/02 Q1010 BLU TEMPO SCT014 GRN"
      }

谢谢

1 个答案:

答案 0 :(得分:0)

我回到基础,我已经开始工作了。我不确定这是最好或最有效的方法,但这就是我所做的。

    $a = 0;
while ($a < sizeof($json_decoded['history']['observations'])) {
    echo $json_decoded['history']['observations'][$a]['date']['hour'] . ":" . $json_decoded['history']['observations'][$a]['date']['min'] . ": ";
    echo $json_decoded['history']['observations'][$a]['tempm'] . "<br>";
    $a++;

}

我心里想到有一个php函数可以帮我这么做,所以我觉得我已经把事情搞得一团糟了。