2017-04-20 28 views
0

內部多維數組我「米試圖遍歷目標i上的」米從openweather.comPHP的爲什麼穿越的foreach()不工作

得到沒有在foreach()I「米獲得正確的結果。在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] => **)) 

任何幫助理解我的失誤可以理解

+0

'$ clima1 [ 'DT']'和'$ clima1 [ '主'] [ '臨時']'。你爲什麼還要在那裏加上'$ i'? – zerkms

+0

只是刪除'[$ i]'? – Philipp

+0

我誤解了var_dump的輸出。我刪除了[$ 1],現在沒關係。謝謝,所有3個答案幫助我瞭解我的錯誤 – ic205

回答

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

這是因爲當你的foreach在$clima['list']你會進去$清風[每值「列表「]。因此,首先,$clima1 = $clima['list'][0]。在此之後,$clima1 = $clima['list'][1] ...因此,$ clima1既沒有0作爲索引,也沒有1也沒有2 ...

你可以做些什麼,看得更清楚的是:

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

而且每次, $鍵將是您的$ id。因此,你可以擺脫你的$ id的,只是不喜歡這樣寫道:

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

當您運行foreach($clima['list'] as $clima1) { ...在環($clima1)每個對象等於​​,所以你不需要手動將$i在那裏。

如果你真的卡住我只是運行像循環:

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

要看到什麼$clima1變量確實是。