0

我有一个从Google Maps API返回的 JSON ,我想只检索州/县/地区和国家/地区。

我使用以下简单代码通过 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);

回显 $data 给了我类似的东西:

{
"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"
}

我想要访问的是多维数组 *address_components* 内部,我假设来自 Google 的每个 JSON 响应每次都会给我相同数量的数组,所以我一直在使用简单的数字索引来访问我需要的数据.

即我认为某些东西$jsondata['results'][0]['address_components'][4]['long_name']总是会给我'administrative_area_level_2'的long_name值 - 在这种情况下是'爱丁堡市',但我错了。

有时地址组件更多,有时更少。这意味着我需要一种搜索​​这个数组然后获取它的索引的方法。

这是怎么做的?如,我如何搜索哪个地址组件的类型为“administrative_area_level_1”,然后返回它的“long_name”值?

4

2 回答 2

3

您可以尝试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
        }
    }
}

这应该是按组件级别循环/搜索组件上的结果集。从那里,您可以处理“当前组件”,您可以对其进行进一步的逻辑/解析。在那个“当前组件”中,你有你正在检查的“类型”。

于 2013-02-28T19:07:08.110 回答
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']);
于 2013-02-28T19:07:20.813 回答