我是php / laravel的初学者。
我有对象数组
这是示例对象:
Geocoder\Provider\Nominatim\Model\NominatimAddress Object
(
[attribution:Geocoder\Provider\Nominatim\Model\NominatimAddress:private] => Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright
[category:Geocoder\Provider\Nominatim\Model\NominatimAddress:private] => boundary
[displayName:Geocoder\Provider\Nominatim\Model\NominatimAddress:private] => Rumo, Comunità della Val di Non, TN, TAA, Italia
[osmType:Geocoder\Provider\Nominatim\Model\NominatimAddress:private] => relation
[osmId:Geocoder\Provider\Nominatim\Model\NominatimAddress:private] => 47201
[type:Geocoder\Provider\Nominatim\Model\NominatimAddress:private] => administrative
[coordinates:Geocoder\Model\Address:private] => Geocoder\Model\Coordinates Object
(
[latitude:Geocoder\Model\Coordinates:private] => 46.4610571
[longitude:Geocoder\Model\Coordinates:private] => 10.9882457
)
[bounds:Geocoder\Model\Address:private] => Geocoder\Model\Bounds Object
(
[south:Geocoder\Model\Bounds:private] => 46.4218546
[west:Geocoder\Model\Bounds:private] => 10.9308997
[north:Geocoder\Model\Bounds:private] => 46.4840451
[east:Geocoder\Model\Bounds:private] => 11.0481174
)
[streetNumber:Geocoder\Model\Address:private] =>
[streetName:Geocoder\Model\Address:private] =>
[subLocality:Geocoder\Model\Address:private] =>
[locality:Geocoder\Model\Address:private] => Rumo
[postalCode:Geocoder\Model\Address:private] =>
[adminLevels:Geocoder\Model\Address:private] => Geocoder\Model\AdminLevelCollection Object
(
[adminLevels:Geocoder\Model\AdminLevelCollection:private] => Array
(
[1] => Geocoder\Model\AdminLevel Object
(
[level:Geocoder\Model\AdminLevel:private] => 1
[name:Geocoder\Model\AdminLevel:private] => TAA
[code:Geocoder\Model\AdminLevel:private] =>
)
[2] => Geocoder\Model\AdminLevel Object
(
[level:Geocoder\Model\AdminLevel:private] => 2
[name:Geocoder\Model\AdminLevel:private] => Comunità della Val di Non
[code:Geocoder\Model\AdminLevel:private] =>
)
)
)
[country:Geocoder\Model\Address:private] => Geocoder\Model\Country Object
(
[name:Geocoder\Model\Country:private] => Italia
[code:Geocoder\Model\Country:private] => IT
)
[timezone:Geocoder\Model\Address:private] =>
[providedBy:Geocoder\Model\Address:private] => nominatim
)
我的php代码:
foreach (app('geocoder')->geocode($request->input('city'))->get() as $key => $value) {
//echo $value->country->code;
echo"<pre>"; print_r($value); echo"</pre>";
}
我要检查国家代码。如果国家/地区代码== PL-那么我要手工显示文本:“这是正确的”;否则,我要显示文本“错误”。
我该怎么做?
我需要结果:
It's correct
It's correct
It's correct
wrong
It's correct
等
我该怎么做?
答案 0 :(得分:1)
要使用Geocoder
获取coutry代码,请使用StringFormatter
类
$formatter = new \Geocoder\Formatter\StringFormatter();
$countryCode = $formatter->format($location, '%c');
中列出了其他有用的格式