PHP Question

What i do wrong with stdClass?


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) } } }

This weather for 5 days ahead, i try to parse this:

foreach ($w as $key=> $value) {
echo $value['weather'][0]['dt'].'<br>';

What i do wrong?


$w is an object and $w->weather is an array. What you should do is iterate the array in foreach. Also, each element of the array is an object too. The other thing is you don't need the key inside the foreach.

Try this:

foreach ($w->weather as $value) {
 echo $value->dt.'<br>';