2012-10-05 113 views
3

我想從他們的郵政編碼(加拿大/美國)的谷歌地理編碼中獲得用戶所在的城市,州/省,國家/地區。我檢索數據爲JSON:https://developers.google.com/maps/documentation/geocoding/#JSONGoogle地理編碼。解析JSON問題

問題是,並不總是相同的數量/順序的'address_components'。我正在使用就像:$geocodedinfo['results'][0]['address_components'][3]['short_name']; 但我很快意識到,這並不總是省/國家,因爲有時谷歌會在'address_components'下添加一個額外的元素。

有沒有一種方法來解析結果只是爲了我需要的細節(即:城市,州/省,國家)?

編輯當我得到它json_decode($result, true)

Array 
(
[results] => Array 
    (
     [0] => Array 
      (
       [address_components] => Array 
        (
         [0] => Array 
          (
           [long_name] => V2X 2P6 
           [short_name] => V2X 2P6 
           [types] => Array 
            (
             [0] => postal_code 
            ) 

          ) 

         [1] => Array 
          (
           [long_name] => Maple Ridge 
           [short_name] => Maple Ridge 
           [types] => Array 
            (
             [0] => locality 
             [1] => political 
            ) 

          ) 

         [2] => Array 
          (
           [long_name] => Maple Ridge 
           [short_name] => Maple Ridge 
           [types] => Array 
            (
             [0] => administrative_area_level_3 
             [1] => political 
            ) 

          ) 

         [3] => Array 
          (
           [long_name] => Greater Vancouver Regional District 
           [short_name] => Greater Vancouver Regional District 
           [types] => Array 
            (
             [0] => administrative_area_level_2 
             [1] => political 
            ) 

          ) 

         [4] => Array 
          (
           [long_name] => British Columbia 
           [short_name] => BC 
           [types] => Array 
            (
             [0] => administrative_area_level_1 
             [1] => political 
            ) 

          ) 

         [5] => Array 
          (
           [long_name] => Canada 
           [short_name] => CA 
           [types] => Array 
            (
             [0] => country 
             [1] => political 
            ) 

          ) 

        ) 

       [formatted_address] => Maple Ridge, BC V2X 2P6, Canada 
       [geometry] => Array 
        (
         [bounds] => Array 
          (
           [northeast] => Array 
            (
             [lat] => 49.2214351 
             [lng] => -122.6577849 
            ) 

           [southwest] => Array 
            (
             [lat] => 49.219268 
             [lng] => -122.663613 
            ) 

          ) 

         [location] => Array 
          (
           [lat] => 49.2202679 
           [lng] => -122.660587 
          ) 

         [location_type] => APPROXIMATE 
         [viewport] => Array 
          (
           [northeast] => Array 
            (
             [lat] => 49.2217005303 
             [lng] => -122.6577849 
            ) 

           [southwest] => Array 
            (
             [lat] => 49.2190025697 
             [lng] => -122.663613 
            ) 

          ) 

        ) 

       [types] => Array 
        (
         [0] => postal_code 
        ) 

      ) 

    ) 

[status] => OK 
) 
+0

我使用的是XML而不是JSON,但我必須迭代,直到找到需要的數據。 –

+0

我有同樣的問題,但冷杉我的情況下,我最需要的address_components所以我用foreach()得到我想要的,忘記其餘的。 –

+0

@ayeshk是否有可能遍歷數組,並獲取['long_name']值WHERE ['types'] ['0'] =='country'for isntance?我從來沒有這樣做過。 – cantaffordretail

回答

4

你將不得不遍歷結果我居然解碼JSON。
大部分相關的PHP函數都不支持多維數組,並且無論如何都會循環。

你可能會想是這樣的:

foreach ($geocodedinfo["results"] as $result) { 
    foreach ($result["address_components"] as $address) { 
     // Repeat the following for each desired type 
     if (in_array("country", $address["types"])) { 
      $country = $address["long_name"]; 
     } 
    } 
} 
+0

這正是我所需要的。現在我知道in_array函數。謝謝。 – cantaffordretail

0

嘗試尋找這個,http://php.net/manual/en/control-structures.foreach.php它是遍歷數組,你可以這樣做,

foreach ($result['results'][0]['address_components'] as $key => $val) 
    { 
    if (in_array('country', $result['results'][0]['address_components'][$key]['types'][0])) 
    { 
    echo $result['results'][0]['address_components'][$key]['long_name']; 
    } 
} 

類似的東西應該工作

+0

這將無法捕捉'types'中的值的順序不同,即'[「political」,「country」] - 您將要使用'in_array'。 – Sara

+0

編輯我的答案來解決這個問題。 – King

0

您可以通過循環運行結果foreach並獲取要查找的數據。

我對Google類型的含義進行了最佳猜測,我可能會忽略這些。

$translate = array(
    'locality' => 'city', 
    'administrative_area_level_3' => 'area', // not sure what this one is 
    'administrative_area_level_2' => 'county', // regional district ? 
    'administrative_area_level_1' => 'state', // province 
    'country' => 'country', 
    'postal_code' => 'zip', // postal code 
); 

$address_components = array(); 
foreach ($geocodedinfo['results'][0]['address_components'] as $component) { 
    $address_components[$translate[$component['type'][0]]] = $component['long_name']; 
} 

var_dump($address_components); 
-1

//解析JSON響應 $jsondata = json_decode($data,true); $results = $jsondata['results'][0]; $addr =$results['formatted_address'];

這允許您訪問長格式的地址

326倫敦路,紐伯裏,伯克希爾,西伯克郡RG14 2DA,英國