php-为什么遍历foreach()内部的多维数组不起作用

时间:2017-04-20 22:42:16

标签: php arrays multidimensional-array foreach

我试图遍历一个对象,我来自openweather.com

没有foreach()我得到了正确的结果。在foreach()里面我得到一个错误。

php代码:

 $contents = file_get_contents($url); 
 $clima=json_decode($contents,TRUE);

 echo $clima['list'][5]['main']['temp'];  // this one works

 $i=0;
 foreach($clima['list'] as $clima1) {
     echo $clima1[$i]['dt'];   // here i get PHP Notice:  Undefined offset

     echo $clima1[$i]['main']['temp']; //here i get PHP Notice:  Undefined index: list

     $i=$i+1;
 }

我得到的错误是:

PHP Notice:  Undefined offset: 0   // the error is from 0 to 39 and there are 39 objects...
PHP Notice:  Undefined index: list 

样本的对象开始和结束部分:

Array ( [cod] => 200 [message] => 0.014 [cnt] => 40 [list] => 
Array ( [0] => Array ( [dt] => 1492732800 [main] => Array ( [temp] => 17.64 [temp_min] => 17.04 [temp_max] => 17.64 [pressure] => 1026.03 [sea_level] => 1031.21 [grnd_level] => 1026.03 [humidity] => 100 [temp_kf] => 0.6 ) [weather] => Array ( [0] => Array ( [id] => 800 [main] => Clear [description] => clear sky [icon] => 01n ) ) [clouds] => Array ( [all] => 0 ) [wind] => Array ( [speed] => 1.66 [deg] => 81.5008 ) [sys] => Array ( [pod] => n ) [dt_txt] => 2017-04-21 00:00:00 ) 
        [38] => Array ( [dt] => 1493143200 [main] => Array ( [temp] => 19.72 [temp_min] => 19.72 [temp_max] => 19.72 [pressure] => 1026.92 [sea_level] => 1032.02 [grnd_level] => 1026.92 [humidity] => 87 [temp_kf] => 0 ) [weather] => Array ( [0] => Array ( [id] => 800 [main] => Clear [description] => clear sky [icon] => 01n ) ) [clouds] => Array ( [all] => 0 ) [wind] => Array ( [speed] => 6.95 [deg] => 10.5008 ) [sys] => Array ( [pod] => n ) [dt_txt] => 2017-04-25 18:00:00 ) 
         [39] => Array ( [dt] => 1493154000 [main] => Array ( [temp] => 18.43 [temp_min] => 18.43 [temp_max] => 18.43 [pressure] => 1026.75 [sea_level] => 1031.91 [grnd_level] => 1026.75 [humidity] => 98 [temp_kf] => 0 ) [weather] => Array ( [0] => Array ( [id] => 800 [main] => Clear [description] => clear sky [icon] => 01n ) ) [clouds] => Array ( [all] => 0 ) [wind] => Array ( [speed] => 6.03 [deg] => 9.50076 ) [sys] => Array ( [pod] => n ) [dt_txt] => 2017-04-25 21:00:00 ) ) 
 [city] => Array ( [id] => **** [name] => ***** [coord] => Array ( [lat] => **.**** [lon] => **.**** ) [country] => ** ) )

任何帮助理解我的错误都将受到赞赏

3 个答案:

答案 0 :(得分:1)

这是因为当你超越$clima['list']时,你将获得$ clima ['list']内的所有值。因此,首先,$clima1 = $clima['list'][0]。之后,$clima1 = $clima['list'][1] ...因此,$ clima1既没有0作为索引也没有1也没有2 ...

你可以做些什么来更清楚地看到它:

foreach($clima['list'] as $key => $clima1)

每次,$ key都是你的$ id。因此,你可以摆脱你的$ id,就这样做:

foreach($clima['list'] as $id => $clima1)

答案 1 :(得分:1)

当您运行foreach($clima['list'] as $clima1) { ...循环中的每个对象($clima1)等于$clima['list'][$i]时,您不需要手动将$i放在那里

如果你真的陷入困境我只是像以下那样运行循环:

foreach($clima['list'] as $clima1) {
    var_dump('<pre>' . $clima1 . '</pre>');
}

要查看$clima1变量到底是什么。

答案 2 :(得分:1)

看来,你循环“双”。循环中的计数器是你不需要的部分,因为你已经使用foreach遍历数组。

我认为,以下代码将是更好的方法

 $contents = file_get_contents($url); 
 $clima=json_decode($contents,TRUE);

 echo $clima['list'][5]['main']['temp'];  // this one works

 foreach($clima['list'] as $clima1) {
     echo $clima1['dt'];   // here i get PHP Notice:  Undefined offset

     echo $clima1['main']['temp']; //here i get PHP Notice:  Undefined index: list
 }