2017-08-13 122 views
-1

我試圖解析來自Google API的響應。解析來自Google地理編碼的json響應

我想知道是否有一種簡單/有效的方法來解析這個數組在php中從'address_components'數組中獲取數據。

{ 
    "results" : [ 
     { 
    "address_components" : [ 
     { 
      "long_name" : "10", 
      "short_name" : "10", 
      "types" : [ "street_number" ] 
     }, 
     { 
      "long_name" : "Downing Street", 
      "short_name" : "Downing St", 
      "types" : [ "route" ] 
     }, 
     { 
      "long_name" : "Westminster", 
      "short_name" : "Westminster", 
      "types" : [ "neighborhood", "political" ] 
     }, 
     { 
      "long_name" : "London", 
      "short_name" : "London", 
      "types" : [ "postal_town" ] 
     }, 
     { 
      "long_name" : "Greater London", 
      "short_name" : "Greater London", 
      "types" : [ "administrative_area_level_2", "political" ] 
     }, 
     { 
      "long_name" : "England", 
      "short_name" : "England", 
      "types" : [ "administrative_area_level_1", "political" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     }, 
     { 
      "long_name" : "SW1A 2AA", 
      "short_name" : "SW1A 2AA", 
      "types" : [ "postal_code" ] 
     } 
    ], 
    "formatted_address" : "10 Downing St, Westminster, London SW1A 2AA, UK", 
    "geometry" : { 
     "location" : { 
      "lat" : 51.5033635, 
      "lng" : -0.1276248 
     }, 
     "location_type" : "ROOFTOP", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 51.5047124802915, 
       "lng" : -0.126275819708498 
      }, 
      "southwest" : { 
       "lat" : 51.5020145197085, 
       "lng" : -0.128973780291502 
      } 
     } 
    }, 
    "place_id" : "ChIJRxzRQcUEdkgRGVaKyzmkgvg", 
    "types" : [ "establishment", "point_of_interest" ] 
    } 
], 
"status" : "OK" 
} 

我最初的方法是爲運行通過陣列一個循環,並創建一個新的陣列和鍵設置爲「類型」,但我遇到了一些問題,其中有多個選項的「類型」

+1

什麼是你想要的輸出? – RamRaider

+0

@RamRaider我的目標是用**「address_components」**數組獲得一個數組,其中**「types」**是關鍵字,而**「long_name」**是數值。 – Callum

+0

JavaScript不支持具有鍵名稱的數組 - – RamRaider

回答

1
$data='{ 
    "results" : [ 
     { 
    "address_components" : [ 
     { 
      "long_name" : "10", 
      "short_name" : "10", 
      "types" : [ "street_number" ] 
     }, 
     { 
      "long_name" : "Downing Street", 
      "short_name" : "Downing St", 
      "types" : [ "route" ] 
     }, 
     { 
      "long_name" : "Westminster", 
      "short_name" : "Westminster", 
      "types" : [ "neighborhood", "political" ] 
     }, 
     { 
      "long_name" : "London", 
      "short_name" : "London", 
      "types" : [ "postal_town" ] 
     }, 
     { 
      "long_name" : "Greater London", 
      "short_name" : "Greater London", 
      "types" : [ "administrative_area_level_2", "political" ] 
     }, 
     { 
      "long_name" : "England", 
      "short_name" : "England", 
      "types" : [ "administrative_area_level_1", "political" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     }, 
     { 
      "long_name" : "SW1A 2AA", 
      "short_name" : "SW1A 2AA", 
      "types" : [ "postal_code" ] 
     } 
    ], 
    "formatted_address" : "10 Downing St, Westminster, London SW1A 2AA, UK", 
    "geometry" : { 
     "location" : { 
      "lat" : 51.5033635, 
      "lng" : -0.1276248 
     }, 
     "location_type" : "ROOFTOP", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 51.5047124802915, 
       "lng" : -0.126275819708498 
      }, 
      "southwest" : { 
       "lat" : 51.5020145197085, 
       "lng" : -0.128973780291502 
      } 
     } 
    }, 
    "place_id" : "ChIJRxzRQcUEdkgRGVaKyzmkgvg", 
    "types" : [ "establishment", "point_of_interest" ] 
    } 
], 
"status" : "OK" 
}'; 

$json=json_decode($data); 
if($json->status=='OK'){ 

    $data=array();/* store results */ 

    $results=$json->results; 
    foreach($results as $index => $obj){ 
     $add=$obj->address_components; 
     foreach($add as $address){ 
      $data[ $obj->formatted_address ][ implode('-', array_values($address->types)) ]=$address->long_name; 
     } 
    } 
    echo '<pre>',print_r($data,1),'</pre>'; 
} 

將輸出:

Array 
(
    [10 Downing St, Westminster, London SW1A 2AA, UK] => Array 
     (
      [street_number] => 10 
      [route] => Downing Street 
      [neighborhood-political] => Westminster 
      [postal_town] => London 
      [administrative_area_level_2-political] => Greater London 
      [administrative_area_level_1-political] => England 
      [country-political] => United Kingdom 
      [postal_code] => SW1A 2AA 
     ) 

) 
+0

[解碼()true version](http://sandbox.onlinephpfunctions.com/code/e16f5168388e79a2f535af3d0d5a2e182ad24f72)(no'array_values()') – mickmackusa

+0

馬的課程 - 最終結果是相同的,雖然我個人更喜歡使用對象表示法而不是數組表示法 – RamRaider

+1

不值得添加一個答案。你的方式非常好。我只是想表現出另一種方式。好答案。 – mickmackusa