Php 格式化 Google 放置输出数组
Php formatting Google places output array
我需要格式化 Google Places 地址的输出。我想删除最后一个分隔线左侧的所有内容,并删除国家(也包含在输出中)。这是一个例子:
Google输出
[address] => Rua da Liberdade - Aparecida, Santos - SP, Brasil
[lat] => -23.9713022
[lng] => -46.3037377
[zoom] => 14
[place_id] => EjBSLiBkYSBMaWJlcmRhZGUgLSBBcGFyZWNpZGEsIFNhbnRvcyAtIFNQLCBCcmF6aWwiLiosChQKEgkfHjkSSwLOlBFOJU9QNBXTDhIUChIJUcEPMUYCzpQRAVML1qqHE18
[name] => Rua da Liberdade
[street_name] => Rua da Liberdade
[street_name_short] => R. da Liberdade
[state] => São Paulo
[state_short] => SP
[country] => Brasil
[country_short] => BR
我需要什么...
Rua da Liberdade - Aparecida, Santos - SP, Brasil
即
Aparecida, Santos - SP
要删除国家/地区,我尝试使用 str_replace
使用密钥 country
:
$locations = get_field( 'profile-location', 'user_' .$userid );
if ( $locations ) {
$location_format = str_replace( $locations['country'], '', $locations['address'] );
}
return $location_format;
您可以从 $locations
数组中提取您想要的信息片段到一个新数组中,跳过不存在的片段。然后可以 implode
d 使输出地址:
$address = [];
if (isset($locations['neighborhood'])) $address[] = $locations['neighborhood'];
if (isset($locations['city'])) $address[] = $locations['city'];
if (isset($locations['state'])) $address[] = $locations['state'];
elseif (isset($locations['state_short'])) $address[] = $locations['state_short'];
$address = implode(', ', $address);
echo $address;
我需要格式化 Google Places 地址的输出。我想删除最后一个分隔线左侧的所有内容,并删除国家(也包含在输出中)。这是一个例子:
Google输出
[address] => Rua da Liberdade - Aparecida, Santos - SP, Brasil
[lat] => -23.9713022
[lng] => -46.3037377
[zoom] => 14
[place_id] => EjBSLiBkYSBMaWJlcmRhZGUgLSBBcGFyZWNpZGEsIFNhbnRvcyAtIFNQLCBCcmF6aWwiLiosChQKEgkfHjkSSwLOlBFOJU9QNBXTDhIUChIJUcEPMUYCzpQRAVML1qqHE18
[name] => Rua da Liberdade
[street_name] => Rua da Liberdade
[street_name_short] => R. da Liberdade
[state] => São Paulo
[state_short] => SP
[country] => Brasil
[country_short] => BR
我需要什么...
Rua da Liberdade - Aparecida, Santos - SP, Brasil
即
Aparecida, Santos - SP
要删除国家/地区,我尝试使用 str_replace
使用密钥 country
:
$locations = get_field( 'profile-location', 'user_' .$userid );
if ( $locations ) {
$location_format = str_replace( $locations['country'], '', $locations['address'] );
}
return $location_format;
您可以从 $locations
数组中提取您想要的信息片段到一个新数组中,跳过不存在的片段。然后可以 implode
d 使输出地址:
$address = [];
if (isset($locations['neighborhood'])) $address[] = $locations['neighborhood'];
if (isset($locations['city'])) $address[] = $locations['city'];
if (isset($locations['state'])) $address[] = $locations['state'];
elseif (isset($locations['state_short'])) $address[] = $locations['state_short'];
$address = implode(', ', $address);
echo $address;