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



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

我得到这个json通过PHP使用以下简单的代码:

$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*,我假设每个JSON响应从谷歌会给我相同数量的数组每次,所以我一直在使用简单的数字索引来访问我需要的数据。

。我认为某些$jsondata['results'][0]['address_components'][4]['long_name']总是会给我'administrative_area_level_2'的long_name值-在本例中是'City of Edinburgh',但是我错了。

有时地址成分较多,有时较少。这意味着我需要一种方法来搜索这个数组然后得到它的索引。

这是怎么做到的?如在,我如何搜索哪个地址组件具有类型"administrative_area_level_1",然后返回"long_name"的值?

您可以尝试通过结果进行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
        }
    }
}

应该按组件级别循环/搜索组件上的结果集。从那里,您有一个"当前组件"的句柄,您可以对其进行进一步的逻辑/解析。

可以使用函数遍历所有数组,如果找到所需的值,则返回所需的值。

$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']);

相关内容

  • 没有找到相关文章

最新更新