所以我有这个数组:
"address_components" : [
{
"long_name" : "#7",
"short_name" : "#7",
"types" : [ "subpremise" ]
},
{
"long_name" : "23",
"short_name" : "23",
"types" : [ "street_number" ]
},
{
"long_name" : "Bedford Road",
"short_name" : "Bedford Rd",
"types" : [ "route" ]
},
{
"long_name" : "Lexington",
"short_name" : "Lexington",
"types" : [ "locality", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "02435",
"short_name" : "02435",
"types" : [ "postal_code" ]
}
],
这段代码:
foreach ('address_components' as $type) {
if ($type['types'] = 'administrative_area_level_1') {
$state = $type['short_name'];
}
}
我正在尝试遍历数组并检查类型是否与管理区域级别1匹配,以及是否确实返回该部分的短名称的值。到目前为止我的代码只返回数组中的第一项。
感谢。
答案 0 :(得分:0)
你的情况不对。改变
if ($type['types'] = 'administrative_area_level_1') // ....
要
if ($type['types'] == 'administrative_area_level_1')
因为if ($type['types'] = 'administrative_area_level_1')
是真的。
您可以使用值:administrative_area_level_1设置$ type [' types'],以便返回true,这是正常的。