var_dump($w);
object(stdClass)#691 (2) { ["name"]=> string(4) "Riga" ["weather"]=> array(7) { [0]=> object(stdClass)#535 (9) { ["w_ico"]=> string(3) "10d" ["dt"]=> int(1470650400) ["temp_day"]=> int(23) ["temp_night"]=> int(19) ["temp_eve"]=> int(23) ["temp_morn"]=> int(23) ["wind_speed"]=> int(8) ["wind_deg"]=> int(231) ["humidity"]=> int(81) } [1]=> object(stdClass)#536 (9) { ["w_ico"]=> string(3) "02d" ["dt"]=> int(1470736800) ["temp_day"]=> int(19) ["temp_night"]=> int(17) ["temp_eve"]=> int(20) ["temp_morn"]=> int(18) ["wind_speed"]=> int(9) ["wind_deg"]=> int(236) ["humidity"]=> int(88) } [2]=> object(stdClass)#537 (9) { ["w_ico"]=> string(3) "10d" ["dt"]=> int(1470823200) ["temp_day"]=> int(18) ["temp_night"]=> int(16) ["temp_eve"]=> int(18) ["temp_morn"]=> int(16) ["wind_speed"]=> int(8) ["wind_deg"]=> int(218) ["humidity"]=> int(94) } [3]=> object(stdClass)#538 (9) { ["w_ico"]=> string(3) "10d" ["dt"]=> int(1470909600) ["temp_day"]=> int(18) ["temp_night"]=> int(15) ["temp_eve"]=> int(16) ["temp_morn"]=> int(16) ["wind_speed"]=> int(5) ["wind_deg"]=> int(207) ["humidity"]=> int(0) } [4]=> object(stdClass)#539 (9) { ["w_ico"]=> string(3) "10d" ["dt"]=> int(1470996000) ["temp_day"]=> int(18) ["temp_night"]=> int(12) ["temp_eve"]=> int(16) ["temp_morn"]=> int(15) ["wind_speed"]=> int(4) ["wind_deg"]=> int(283) ["humidity"]=> int(0) } [5]=> object(stdClass)#540 (9) { ["w_ico"]=> string(3) "10d" ["dt"]=> int(1471082400) ["temp_day"]=> int(18) ["temp_night"]=> int(13) ["temp_eve"]=> int(17) ["temp_morn"]=> int(15) ["wind_speed"]=> int(2) ["wind_deg"]=> int(282) ["humidity"]=> int(0) } [6]=> object(stdClass)#543 (9) { ["w_ico"]=> string(3) "10d" ["dt"]=> int(1471168800) ["temp_day"]=> int(20) ["temp_night"]=> int(16) ["temp_eve"]=> int(19) ["temp_morn"]=> int(16) ["wind_speed"]=> int(6) ["wind_deg"]=> int(201) ["humidity"]=> int(0) } } }
这个天气提前5天,我试着解析一下:
foreach ($w as $key=> $value) {
echo $value['weather'][0]['dt'].'<br>';
}
我做错了什么?
答案 0 :(得分:1)
$ w是一个对象,$ w-&gt; weather是一个数组。你应该做的是在foreach中迭代数组。此外,数组的每个元素也是一个对象。另一件事是你不需要foreach中的密钥。
试试这个:
foreach ($w->weather as $value) {
echo $value->dt.'<br>';
}
答案 1 :(得分:0)
这是一个对象,试试这个:
$value->weather[0]->dt;