Php格式化Google Places输出数组

问题描述 投票:-2回答:1

我需要格式化Google地方信息地址的输出。我想删除最后一条分隔线左侧的所有内容,并删除国家(也包含在输出中)。这是一个例子:

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-SP ,巴西

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;
php google-maps google-places-api
1个回答
1
投票

您可以将所需的信息从$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;

Demo on 3v4l.org

© www.soinside.com 2019 - 2024. All rights reserved.