2012-12-03 53 views
0

這看起來像是一個新手問題,但我只是在這裏敲擊鍵盤,我找不到任何已經回答的問題讓我前進。使用PHP從Google Maps API讀取對象數組

情景是我試圖通過使用Google地圖API對其進行地理編碼來獲取郵政編碼的緯度/經度。我已將Google Maps API的結果作爲JSON字符串返回,並使用json_decode將其放入PHP數組中。但它看起來像是一個對象數組,我很難理解如何深入數據來獲取我的經緯度值。

這是當前道路塊......代碼,那麼結果:

<?php 
$jsonData = '{"results":[{"address_components":[{"long_name":"33647","short_name":"33647","types":["postal_code"]},{"long_name":"Tampa","short_name":"Tampa","types":["locality","political"]},{"long_name":"Florida","short_name":"FL","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"Tampa, FL 33647, USA","geometry":{"bounds":{"northeast":{"lat":28.17150,"lng":-82.26235779999999},"southwest":{"lat":28.07291710,"lng":-82.42569910}},"location":{"lat":28.14343180,"lng":-82.33433749999999},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":28.17150,"lng":-82.26235779999999},"southwest":{"lat":28.07291710,"lng":-82.42569910}}},"types":["postal_code"]}],"status":"OK"}'; 
$phpArray = json_decode($jsonData); 
print_r($phpArray); 

foreach ($phpArray as $key => $value) { 
    echo "<p>$key | $value</p>"; 
} 
?> 

結果:

stdClass Object ([results] => Array ([0] => stdClass Object ([address_components] => Array ([0] => stdClass Object ([long_name] => 33647 [short_name] => 33647 [types] => Array ([0] => postal_code)) [1] => stdClass Object ([long_name] => Tampa [short_name] => Tampa [types] => Array ([0] => locality [1] => political)) [2] => stdClass Object ([long_name] => Florida [short_name] => FL [types] => Array ([0] => administrative_area_level_1 [1] => political)) [3] => stdClass Object ([long_name] => United States [short_name] => US [types] => Array ([0] => country [1] => political))) [formatted_address] => Tampa, FL 33647, USA [geometry] => stdClass Object ([bounds] => stdClass Object ([northeast] => stdClass Object ([lat] => 28.1715 [lng] => -82.2623578) [southwest] => stdClass Object ([lat] => 28.0729171 [lng] => -82.4256991)) [location] => stdClass Object ([lat] => 28.1434318 [lng] => -82.3343375) [location_type] => APPROXIMATE [viewport] => stdClass Object ([northeast] => stdClass Object ([lat] => 28.1715 [lng] => -82.2623578) [southwest] => stdClass Object ([lat] => 28.0729171 [lng] => -82.4256991))) [types] => Array ([0] => postal_code))) [status] => OK) 

results | Array 

status | OK 

URL用於創建輸入JSON:

http://maps.googleapis.com/maps/api/geocode/json?address=33647&sensor=false

尋找一些幫助,將Lat和Long值拉出到PHP vari中能夠。

在此先感謝! Josh

回答

1

結果的值實際上是另一個數組 - 因此您需要深入數組以獲取所需的值。

此頁面(http://json.parser.online.fr/)可能會幫助您更清楚地顯示數據。

這裏與您的數據,以證實深度(數組作爲值),一個可怕的例子:

<?php 
$jsonData = '{"results":[{"address_components":[{"long_name":"33647","short_name":"33647","types":["postal_code"]},{"long_name":"Tampa","short_name":"Tampa","types":["locality","political"]},{"long_name":"Florida","short_name":"FL","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"Tampa, FL 33647, USA","geometry":{"bounds":{"northeast":{"lat":28.17150,"lng":-82.26235779999999},"southwest":{"lat":28.07291710,"lng":-82.42569910}},"location":{"lat":28.14343180,"lng":-82.33433749999999},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":28.17150,"lng":-82.26235779999999},"southwest":{"lat":28.07291710,"lng":-82.42569910}}},"types":["postal_code"]}],"status":"OK"}'; 
$phpArray = json_decode($jsonData,true); 
print_r($phpArray); 

foreach ($phpArray as $key => $value) { 
    if ($key == "results") { 
     foreach ($value as $key2 => $value2) { 
      foreach ($value2 as $key3 => $value3) { 
       echo "<p>$key3 | $value3</p>"; 
      } 
     } 
    } 
} 
?> 

你需要向下挖了幾個級別的找到你想要的LL數據。這應該指出你在正確的方向。

+0

謝謝拉斯......這確實讓我走上了正確的道路。作爲另一種選擇,我挖得更深一點,發現這有助於我深入瞭解陣列或陣列:-) $ fullurl =「http://maps.googleapis.com/maps/api/geocode/json?address = 33647&傳感器=假「; $ string = file_get_contents($ fullurl); //獲取json內容 $ json_a = json_decode($ string,true); // json decoder echo $ json_a ['results'] [0] ['geometry'] ['location'] ['lat']; echo $ json_a ['results'] [0] ['geometry'] ['location'] ['lng']; – Josh