【问题标题】:php- why traversing multidimensional array inside the foreach() doesnt workphp-为什么在 foreach() 中遍历多维数组不起作用
【发布时间】:2017-04-20 22:42:15
【问题描述】:

我正在尝试遍历我从 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] => ** ) )

任何帮助理解我的错误将不胜感激

【问题讨论】:

  • $clima1['dt']$clima1['main']['temp']。你为什么还要在那里添加$i
  • 只删除[$i] ?
  • 我误解了 var_dump 的输出。我删除了 [$1],现在可以了。谢谢,所有 3 个答案都帮助我理解了我的错误

标签: php arrays multidimensional-array foreach


【解决方案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
 }

【讨论】:

    【解决方案2】:

    那是因为当你遍历$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)
    

    【讨论】:

      【解决方案3】:

      当您运行 foreach($clima['list'] as $clima1) { ... 时,循环中的每个对象 ($clima1) 都等于 $clima['list'][$i],因此您无需手动将 $i 放入其中。

      如果你真的卡住了,我会像这样运行循环:

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

      查看$clima1 变量的真正含义。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-24
        • 1970-01-01
        • 2021-09-17
        • 2012-07-17
        • 1970-01-01
        • 2020-09-06
        • 1970-01-01
        相关资源
        最近更新 更多