每当我尝试遍历我的json数据时,我只能显示最后一个数组而不是所有数组,这是我想要的。
我有以下json数据
{"sunSpecPlantExtract": {"t":"2016-07-06T13:11:35", "plant":{ "id":"8eefcfdf5990e441f0fb6f3fad709e21", "v":"1", "locale":"en-US"}, "name":"White Rose Foods", "location>":{"line1":"380 Middlesex Ave", "city":"Carteret", "state":"NJ", "postal":"07008", "timezone":"-5.00" }, "sunSpecData":{"v":"1","periodStart":"2016-07-06T05:00:00","periodEnd=":"2016-07-06T12:15:00" ,"d":{ "man":"Shark","t":"2016-07-06T05:00:00", "m":{ "id":"6519", "sn":"0083574330" ,"WHL":"27400631402" ,"W":"-15500" ,"PPVphAB":"13286" ,"PPVphBC":"13220" ,"PPVphCA":"13253" ,"A":"0.761" ,"WH":"0" } } ,"d":{ "man":"Shark","t":"2016-07-06T05:15:00", "m":{ "id":"6519", "sn":"0083574330" ,"WHL":"27400631402" ,"W":"-15940" ,"PPVphAB":"13294" ,"PPVphBC":"13237" ,"PPVphCA":"13265" ,"A":"0.778" ,"WH":"0" } } ,"d":{ "man":"Shark","t":"2016-07-06T05:30:00", "m":{ "id":"6519", "sn":"0083574330" ,"WHL":"27400631402" ,"W":"-16114" ,"PPVphAB":"13290" ,"PPVphBC":"13244" ,"PPVphCA":"13267" ,"A":"0.789" ,"WH":"0" } } ,"d":{ "man":"Shark","t":"2016-07-06T05:45:00", "m":{ "id":"6519", "sn":"0083574330" ,"WHL":"27400631402" ,"W":"-416" ,"PPVphAB":"13240" ,"PPVphBC":"13183" ,"PPVphCA":"13212" ,"A":"0.74" ,"WH":"0" } } ,"d":{ "man":"SMAINVERTER","t":"2016-07-06T05:00:00", "m":{ "id":"6520", "sn":"6520" ,"DCV":"105" ,"W":"0" ,"DCA":"0" ,"WHL":"3685975455" ,"WH":"0" } } ,"d":{ "man":"SMAINVERTER","t":"2016-07-06T05:15:00", "m":{ "id":"6520", "sn":"6520" ,"DCV":"323" ,"W":"0" ,"DCA":"0" ,"WHL":"3685975455" ,"WH":"0" } } ,"d":{ "man":"SMAINVERTER","t":"2016-07-06T05:30:00", "m":{ "id":"6520", "sn":"6520" ,"DCV":"390" ,"W":"0" ,"DCA":"0" ,"WHL":"3685975455" ,"WH":"0" } } ,"d":{ "man":"SMAINVERTER","t":"2016-07-06T05:45:00", "m":{ "id":"6520", "sn":"6520" ,"DCV":"341" ,"W":"3800" ,"DCA":"12" ,"WHL":"3685975455" ,"WH":"0" } } ,"d":{ "man":"SMAINVERTER","t":"2016-07-06T06:00:00", "m":{ "id":"6520", "sn":"6520" ,"DCV":"355" ,"W":"11500" ,"DCA":"34" ,"WHL":"3685976155" ,"WH":"699" } } ,"d":{ "man":"SMAINVERTER","t":"2016-07-06T06:15:00", "m":{ "id":"6520", "sn":"6520" ,"DCV":"392" ,"W":"25800" ,"DCA":"68" ,"WHL":"3685980555" ,"WH":"4400" } } ,"d":{ "man":"SMAINVERTER","t":"2016-07-06T06:30:00", "m":{ "id":"6520", "sn":"6520" ,"DCV":"400" ,"W":"47400" ,"DCA":"122" ,"WHL":"3685989655" ,"WH":"9099" } } ,"d":{ "man":"SMAINVERTER","t":"2016-07-06T06:45:00", "m":{ "id":"6520", "sn":"6520" ,"DCV":"400" ,"W":"70800" ,"DCA":"183" ,"WHL":"3686004055" ,"WH":"14400" } } ,"d":{ "man":"SMAINVERTER","t":"2016-07-06T07:00:00", "m":{ "id":"6520", "sn":"6520" ,"DCV":"408" ,"W":"96800" ,"DCA":"245" ,"WHL":"3686025155" ,"WH":"21099" } } ,"d":{ "man":"SMAINVERTER","t":"2016-07-06T07:15:00", "m":{ "id":"6520", "sn":"6520" ,"DCV":"396" ,"W":"126900" ,"DCA":"331" ,"WHL":"3686053155" ,"WH":"28000" } } ,"d":{ "man":"SMAINVERTER","t":"2016-07-06T07:30:00", "m":{ "id":"6520", "sn":"6520" ,"DCV":"392" ,"W":"151100" ,"DCA":"398" ,"WHL":"3686088655" ,"WH":"35500" } } } } } }
然后我有以下代码来读取json数据
$data = json_decode($json_data, true);
现在这是我试图循环json数据
foreach($data as $value){
echo "Inverter: " . $value['sunSpecData']['d']['man'] . "<br>";
echo "DateTime: " . $value['sunSpecData']['d']['t'] . "<br>";
echo "W: " . $value['sunSpecData']['d']['m']['W'] . "<br><br>";
}
我只得到最后一个数组
Inverter: SMAINVERTER
DateTime: 2016-07-06T07:30:00
W: 151100
我想要显示所有数组而不是最后一个。像这样:
Inverter: Shark
DateTime: 2016-07-06T05:00:00
W: -15500
Inverter: Shark
DateTime: 2016-07-06T05:15:00
W: -15940
....and so on until the last one....
Inverter: SMAINVERTER
DateTime: 2016-07-06T07:30:00
W: 151100
答案 0 :(得分:0)
只有1个sunSpecData要迭代。您想要遍历该值本身。
foreach($data['sunSpecPlantExtract']['sunSpecData'] as $k => $value){ // this will spit out each value
if($k == "d") { // to skip 'v', 'periodStart' and 'periodEnd'
echo "Inverter: " . $value['man'] . "<br>";
echo "DateTime: " . $value['t'] . "<br>";
echo "W: " . $value['m']['W'] . "<br><br>";
}
}