Php格式化Google Places输出数组

问题描述 投票:-3回答: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 ,巴西

要删除国家/地区,我尝试在主要国家/地区旁边使用str_replace进行可能的更改。

[Aparecida,桑托斯-SP

我的代码...

$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来设置输出地址:
© www.soinside.com 2019 - 2024. All rights reserved.