2013-02-28 72 views
0

我有一個JSON從Google Maps API返回,我只想要檢索州/縣/地區和國家。如何搜索包含某些內容的數組中的值?

我用下面簡單的代碼通過PHP得到這個JSON:

$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=the+address&sensor=true&key=my+api+key'; 

// make the HTTP request 
$data = @file_get_contents($url); 

$jsondata = json_decode($data,true); 

呼應$的數據給了我這樣的:

{ 
"results" : [ 
    { 
    "address_components" : [ 
     { 
      "long_name" : "9", 
      "short_name" : "9", 
      "types" : [ "street_number" ] 
     }, 
     { 
      "long_name" : "Hanover Street", 
      "short_name" : "Hanover St", 
      "types" : [ "route" ] 
     }, 
     { 
      "long_name" : "Edinburgh", 
      "short_name" : "Edinburgh", 
      "types" : [ "locality", "political" ] 
     }, 
     { 
      "long_name" : "City of Edinburgh", 
      "short_name" : "City of Edinburgh", 
      "types" : [ "administrative_area_level_2", "political" ] 
     }, 
     { 
      "long_name" : "Scotland", 
      "short_name" : "Scotland", 
      "types" : [ "administrative_area_level_1", "political" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     }, 
     { 
      "long_name" : "EH2", 
      "short_name" : "EH2", 
      "types" : [ "postal_code_prefix", "postal_code" ] 
     }, 
     { 
      "long_name" : "Edinburgh", 
      "short_name" : "Edinburgh", 
      "types" : [ "postal_town" ] 
     } 
    ] 
    } 
], 
"status" : "OK" 
} 

什麼,我想訪問多維內array * address_components *,並且我認爲每次來自Google的JSON響應都會給我相同數量的數組,所以我一直使用簡單的數字索引來訪問我需要的數據。

即我認爲某些$jsondata['results'][0]['address_components'][4]['long_name']總會給我'的行政_area_level_2'的long_name值 - 在這種情況下,'愛丁堡市',但我錯了。

有時會有更多的地址組件,有時甚至更少。這意味着我需要一種搜索​​這個數組的方法,然後獲取它的索引。

這是怎麼做的?如在中,我如何搜索哪個地址組件的類型爲'administrative_area_level_1',然後返回那個'long_name'值?

回答

2

你可以嘗試的是做一個foreach循環遍歷結果,檢查你所需要的數據。如果您正在尋找其中一個地址組件內的「administrative_area_level_1」,則可以檢查它是否爲type

事情是這樣的:

foreach ($jsondata['results'][0]['address_components'] as $comp) { 
    //loop through each component in ['address_components'] 
    foreach ($comp['types'] as $currType){ 
     //for every type in the current component, check if it = the check 
     if($currType == 'administrative_area_level_1'){ 
      echo $comp['long_name']; 
      //Do whatever with the component, print longname, whatever you need 
      //You can add $comp into another array to have an array of 'administrative_area_level_1' types 
     } 
    } 
} 

這應該循環/通過搜索結果通過設置組件級別的組件上。從那裏,你有一個「當前組件」的句柄,你可以做進一步的邏輯/解析。在「當前組件」裏面,你有你正在檢查的「類型」。

1

您可以使用函數遍歷數組並返回所需的值(如果找到它)。

$jsondata = json_decode($data, true); 

function getAdministrativeAreaLevel2($addresses) { 
    if (!is_array($addresses) || empty($addresses)) { 
     return; // we need an array with data 
    } 

    foreach ($addresses as $address) { 
     if (!isset($address['address_components'])) 
      continue; // nothing to look at 
     foreach ($address['address_components'] as $compontent) { 
      // should be an array with types 
      if (is_array($compontent['types']) && !empty($compontent['types'])) { 
       foreach ($compontent['types'] as $type) { 
        if ($type == 'administrative_area_level_2') { 
         return $compontent['long_name']; 
        } 
       } 
      } 
     } 
    } 
} 
echo getAdministrativeAreaLevel2($jsondata['results']); 
相關問題