2012-02-02 122 views
-3

我不是一個PHP開發人員。我正在使用谷歌API,我使用的後端是PHP。我正在使用一個返回給定郵政編碼的特定距離內的位置的例子。從PHP返回的結果集的格式如下..循環PHP陣列

Array 
(
[0] => Array 
(
[id] => 554 
[postcode] => 1225 
[suburb] => Royal Exchange 
[aus_post_name] => ROYAL EXCHANGE 
[state] => NSW 
[lat] => -33.864393 
[lon] => 151.209608 
) 

[1] => Array 
(
[id] => 559 
[postcode] => 1230 
[suburb] => Queen Victoria Building 
[aus_post_name] => QUEEN VICTORIA BUILDING 
[state] => NSW 
[lat] => -33.872049 
[lon] => 151.206428 
) 

[2] => Array 
(
[id] => 714 
[postcode] => 2000 
[suburb] => Barangaroo 
[aus_post_name] => BARANGAROO 
[state] => NSW 
[lat] => -33.858315 
[lon] => 151.203519 
) 

[3] => Array 
(
[id] => 715 
[postcode] => 2000 
[suburb] => Dawes Point 
[aus_post_name] => DAWES POINT 
[state] => NSW 
[lat] => -33.855601 
[lon] => 151.20822 
) 

我如何在結果集中循環,並獲得價值,例如用於國家和郊區?

+1

顯示一些代碼,如@JackManey所述。 – Cyclone 2012-02-02 03:32:03

+0

沒關係......自己拿到了答案。繼續投票並關閉它.. – DG3 2012-02-02 03:39:01

+0

使用foreach循環 – alecwhardy 2012-02-02 04:33:41

回答

3

使用for loopforeach loop

foreach ($bigarray as $row) { 
    echo $row['suburb']; // suburb 
    echo $row['state']; // state 
} 
+2

呃什麼?它應該是'foreach($ bigarray as $ row)',不是?你目前的代碼沒有意義。不降低表決的原因是它的一個小錯誤,但不是upvoting導致它現在是錯誤的。 – Cyclone 2012-02-02 03:35:06

+0

糟糕!乾杯。 – 2012-02-02 03:45:14

0

使用循環。這是獲取州和郊區數據的最佳方式..以下代碼將幫助您...

$arr_var=Array 
(
    0 => Array 
    (
     'id' => 554, 
     'postcode' => 1225, 
     'suburb' => 'Royal Exchange', 
     'aus_post_name' =>'ROYAL EXCHANGE', 
     'state' => 'NSW', 
     'lat' => -33.864393, 
     'lon' => 151.209608 
    ), 

1 => Array 
    (
     'id' => 559, 
     'postcode' => 1230, 
     'suburb' => 'Queen Victoria Building', 
     'aus_post_name' => 'QUEEN VICTORIA BUILDING', 
     'state' => 'NSW', 
     'lat' => -33.872049, 
     'lon' => 151.206428, 
    ) 
); 

for($i=0;count($arr_var)>$i;$i++) 
{ 
    echo " state : ".$arr_var[$i]['state']; 
    echo " suburb : ".$arr_var[$i]['suburb']; 
}