2013-01-15 158 views
0

我有一個問題,得到數組值「lat」和「long」,而不會深入foreach函數。陣列:PHP的多維數組 - foreach循環

array(1) { 
    ["Berlin, Germany(All airports)"]=> 
    array(1) { 
    ["Berlin Brandenburg Willy Brandt(BER)"]=> 
    array(2) { 
     ["lat"]=> 
     string(9) "52.366667" 
     ["lon"]=> 
     string(9) "13.503333" 
    } 
    } 

}

功能:

foreach($results as $key => $val){ 
    //here i want to reach lat and long without additional foreach loop 
} 

謝謝大家的答案。

+0

您可能需要使用:[array_walk_recursive](http://stackoverflow.com/questions/526556/how-to-flatten-a-multi-dimensional-array-to-simple-one-in- php) –

+0

請注意,BER不會打開一段時間;-)悲傷的故事。 – johannes

回答

6
foreach($results as $key => $val){ 
    $temp = current($val); # fetch first value, which is array in your example 
    echo $temp['lat']; 
} 
+1

不應該是'$ val = current($ val)'或$ temp ['lat']'? – Supericy

+1

@超級錯誤,我修好了。 –

+0

比你多,工作正常....是的,$ temp soulh是$ val – user1974467