解析来自谷歌地理编码的 json 响应



我正在尝试解析来自Google API的响应。

我想知道是否有一种简单/有效的方法可以在 php 中解析此数组以从"address_components"数组中获取数据。

{
"results" : [
{
"address_components" : [
{
"long_name" : "10",
"short_name" : "10",
"types" : [ "street_number" ]
},
{
"long_name" : "Downing Street",
"short_name" : "Downing St",
"types" : [ "route" ]
},
{
"long_name" : "Westminster",
"short_name" : "Westminster",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "London",
"short_name" : "London",
"types" : [ "postal_town" ]
},
{
"long_name" : "Greater London",
"short_name" : "Greater London",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "England",
"short_name" : "England",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
},
{
"long_name" : "SW1A 2AA",
"short_name" : "SW1A 2AA",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "10 Downing St, Westminster, London SW1A 2AA, UK",
"geometry" : {
"location" : {
"lat" : 51.5033635,
"lng" : -0.1276248
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 51.5047124802915,
"lng" : -0.126275819708498
},
"southwest" : {
"lat" : 51.5020145197085,
"lng" : -0.128973780291502
}
}
},
"place_id" : "ChIJRxzRQcUEdkgRGVaKyzmkgvg",
"types" : [ "establishment", "point_of_interest" ]
}
],
"status" : "OK"
}

我最初的方法是在数组中运行循环并创建一个新数组并将键设置为"类型",但我遇到了一些问题,其中"类型"有多个选项

$data='{
"results" : [
{
"address_components" : [
{
"long_name" : "10",
"short_name" : "10",
"types" : [ "street_number" ]
},
{
"long_name" : "Downing Street",
"short_name" : "Downing St",
"types" : [ "route" ]
},
{
"long_name" : "Westminster",
"short_name" : "Westminster",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "London",
"short_name" : "London",
"types" : [ "postal_town" ]
},
{
"long_name" : "Greater London",
"short_name" : "Greater London",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "England",
"short_name" : "England",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
},
{
"long_name" : "SW1A 2AA",
"short_name" : "SW1A 2AA",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "10 Downing St, Westminster, London SW1A 2AA, UK",
"geometry" : {
"location" : {
"lat" : 51.5033635,
"lng" : -0.1276248
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 51.5047124802915,
"lng" : -0.126275819708498
},
"southwest" : {
"lat" : 51.5020145197085,
"lng" : -0.128973780291502
}
}
},
"place_id" : "ChIJRxzRQcUEdkgRGVaKyzmkgvg",
"types" : [ "establishment", "point_of_interest" ]
}
],
"status" : "OK"
}';
$json=json_decode( $data );
if( $json->status=='OK' ){
$data=array();/* store results */
$results=$json->results;
foreach( $results as $index => $obj ){
$add=$obj->address_components;
foreach( $add as $address ){
$data[ $obj->formatted_address ][ implode( '-', array_values( $address->types ) ) ]=$address->long_name;
}
}
echo '<pre>',print_r($data,1),'</pre>';
}

将输出:

Array
(
[10 Downing St, Westminster, London SW1A 2AA, UK] => Array
(
[street_number] => 10
[route] => Downing Street
[neighborhood-political] => Westminster
[postal_town] => London
[administrative_area_level_2-political] => Greater London
[administrative_area_level_1-political] => England
[country-political] => United Kingdom
[postal_code] => SW1A 2AA
)
)

相关内容

  • 没有找到相关文章

最新更新