获得满足条件的更深层元素
get a deeper element which satisfies the condition
"address_components": [
{
"long_name": "8",
"short_name": "8",
"types": [
"street_number"
]
},
{
"long_name": "Promenade",
"short_name": "Promenade",
"types": [
"route"
]
},
{
"long_name": "Cheltenham",
"short_name": "Cheltenham",
"types": [
"postal_town"
]
},
{
"long_name": "Gloucestershire",
"short_name": "Gloucestershire",
"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": "GL50 1LR",
"short_name": "GL50 1LR",
"types": [
"postal_code"
]
}
],
需要获取postal_code值,即types=postal_code的long_name的值。在 api 结果中,类型本身似乎是一个数组。循环查找是一个不好的方法。也 array_search 也不起作用。谁能帮帮我
只需遍历数组并找到带有邮政编码的项目:
foreach ($arr["address_components"] as $item) {
if (in_array("postal_code", $item["types"])) {
echo $item["long_name"];
}
}
我会把错误处理留给你。
您可以使用 array_filter
来迭代数组而无需循环:
$postal_code_arrays = array_filter($arr, function($a){
if(!isset($a['types'])) return false;
// Or you can use another condition. i.e: if array only contains postal code
if(in_array('postal_code', $a['types'])) {
return true;
}
return false;
});
这将 return 只包含数组中最后一个的数组:
[
[
"long_name" => "GL50 1LR",
"short_name" => "GL50 1LR",
"types" => [
"postal_code"
]
]
]
尝试array_filter
$postCode = array_filter($arr["address_components"], function($v) {
return in_array("postal_code", $v["types"]);
})[0]['long_name'];
"address_components": [
{
"long_name": "8",
"short_name": "8",
"types": [
"street_number"
]
},
{
"long_name": "Promenade",
"short_name": "Promenade",
"types": [
"route"
]
},
{
"long_name": "Cheltenham",
"short_name": "Cheltenham",
"types": [
"postal_town"
]
},
{
"long_name": "Gloucestershire",
"short_name": "Gloucestershire",
"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": "GL50 1LR",
"short_name": "GL50 1LR",
"types": [
"postal_code"
]
}
],
需要获取postal_code值,即types=postal_code的long_name的值。在 api 结果中,类型本身似乎是一个数组。循环查找是一个不好的方法。也 array_search 也不起作用。谁能帮帮我
只需遍历数组并找到带有邮政编码的项目:
foreach ($arr["address_components"] as $item) {
if (in_array("postal_code", $item["types"])) {
echo $item["long_name"];
}
}
我会把错误处理留给你。
您可以使用 array_filter
来迭代数组而无需循环:
$postal_code_arrays = array_filter($arr, function($a){
if(!isset($a['types'])) return false;
// Or you can use another condition. i.e: if array only contains postal code
if(in_array('postal_code', $a['types'])) {
return true;
}
return false;
});
这将 return 只包含数组中最后一个的数组:
[
[
"long_name" => "GL50 1LR",
"short_name" => "GL50 1LR",
"types" => [
"postal_code"
]
]
]
尝试array_filter
$postCode = array_filter($arr["address_components"], function($v) {
return in_array("postal_code", $v["types"]);
})[0]['long_name'];