【发布时间】:2017-08-13 06:22:30
【问题描述】:
我正在尝试解析来自 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"
}
我最初的方法是循环遍历数组并创建一个新数组并将键设置为“类型”,但我遇到了一些问题,即“类型”有多个选项
【问题讨论】:
-
你想要的输出是什么?
-
@RamRaider 我的目标是获得一个包含 "address_components" 数组的数组,其中 "types" 将是键,而 " long_name" 将是值。
-
javascript 不支持带有键名的数组 -
-
@RamRaider 我正在使用 PHP,所以响应是一个 json 数组并且将其输出到 assoc 数组
-
而
types有多个值,您会使用它们作为键吗?
标签: php arrays json google-maps google-api