如何在包含某些内容的数组中搜索值?

时间:2013-02-28 18:50:41

标签: php json google-maps-api-3 multidimensional-array

我从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”值?

2 个答案:

答案 0 :(得分: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
        }
    }
}

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

答案 1 :(得分: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']);