Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 谷歌地理编码。解析JSON问题_Php_Json_Geocode - Fatal编程技术网

Php 谷歌地理编码。解析JSON问题

Php 谷歌地理编码。解析JSON问题,php,json,geocode,Php,Json,Geocode,我想从谷歌的邮政编码/邮政编码(加拿大/美国)中获取用户的城市、州/省、国家/地区。我以JSON的形式检索数据: 问题在于“地址组件”的数量/顺序并不总是相同的。我使用的方法就像:$geocodedinfo['results'][0]['address\u components'][3]['short\u name'] 但我很快意识到,这并不总是省/州,因为有时谷歌会在“地址组件”下添加一个额外的元素 是否有一种方法可以仅针对我需要的具体情况(即:城市、州/省、国家)分析结果 EDIT当我得到J

我想从谷歌的邮政编码/邮政编码(加拿大/美国)中获取用户的城市、州/省、国家/地区。我以JSON的形式检索数据:

问题在于“地址组件”的数量/顺序并不总是相同的。我使用的方法就像:
$geocodedinfo['results'][0]['address\u components'][3]['short\u name']
但我很快意识到,这并不总是省/州,因为有时谷歌会在“地址组件”下添加一个额外的元素

是否有一种方法可以仅针对我需要的具体情况(即:城市、州/省、国家)分析结果

EDIT当我得到JSON时,我实际上解码了它
JSON\u decode($result,true)


您必须对结果进行循环。
大多数相关的PHP函数不支持多维数组,而支持多维数组的函数无论如何都会循环

你可能会想要这样的东西:

foreach ($geocodedinfo["results"] as $result) {
    foreach ($result["address_components"] as $address) {
        // Repeat the following for each desired type
        if (in_array("country", $address["types"])) {
            $country = $address["long_name"];
        }
    }
}
试着看看这个,它是用来在数组上循环的,你可以做如下的事情

foreach ($result['results'][0]['address_components'] as $key => $val) 
    { 
    if (in_array('country', $result['results'][0]['address_components'][$key]['types'][0])) 
    { 
    echo $result['results'][0]['address_components'][$key]['long_name']; 
    } 
}

类似的方法应该可以运行

您可以通过一个
foreach
循环运行结果,并获取您正在查找的数据

我正在对谷歌类型的含义进行最好的猜测,我可能会对这些类型感兴趣

$translate = array(
    'locality' => 'city',
    'administrative_area_level_3' => 'area', // not sure what this one is
    'administrative_area_level_2' => 'county', // regional district ?
    'administrative_area_level_1' => 'state', // province
    'country' => 'country',
    'postal_code' => 'zip', // postal code
);

$address_components = array( );
foreach ($geocodedinfo['results'][0]['address_components'] as $component) {
    $address_components[$translate[$component['type'][0]]] = $component['long_name'];
}

var_dump($address_components);
//解析json响应
$jsondata=json\u decode($data,true)
$results=$jsondata['results'][0]
$addr=$results['formatted_address']

这允许您访问长格式的地址


英国伯克郡西伯克郡纽伯里伦敦路326号RG14 2DA

我使用XML而不是JSON,但我必须反复使用,直到找到一个包含我需要的数据的。我也遇到了同样的问题,但首先我需要大部分的address_组件,所以我使用了foreach()为了得到我想要的,而忘记剩下的。@ayeshk是否可以在数组上循环,只获取['long_name']值,其中['types']['0']=='country'表示iNtance?我以前从未这样做过。如果
类型
中的值顺序不同,即
[“政治”、“国家”]
,则无法捕获结果。您将希望在数组中使用
。编辑我的答案以说明这一点。这正是我需要的。现在我知道了in_数组函数。谢谢
$translate = array(
    'locality' => 'city',
    'administrative_area_level_3' => 'area', // not sure what this one is
    'administrative_area_level_2' => 'county', // regional district ?
    'administrative_area_level_1' => 'state', // province
    'country' => 'country',
    'postal_code' => 'zip', // postal code
);

$address_components = array( );
foreach ($geocodedinfo['results'][0]['address_components'] as $component) {
    $address_components[$translate[$component['type'][0]]] = $component['long_name'];
}

var_dump($address_components);