如何从php中的对象获取国家代码?

时间:2019-08-30 12:58:29

标签: php laravel laravel-5

我是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

我该怎么做?

1 个答案:

答案 0 :(得分:1)

要使用Geocoder获取coutry代码,请使用StringFormatter

$formatter = new \Geocoder\Formatter\StringFormatter();

$countryCode = $formatter->format($location, '%c');

Documentation

中列出了其他有用的格式
  • 街道号:%n
  • 街道名称:%S
  • 城市(位置):%L
  • 市区(子地区):%D
  • 邮政编码(邮政编码):%z
  • 管理员级别名称:%A1,%A2,%A3,%A4,%A5
  • 管理员级别代码:%a1,%a2,%a3,%a4,%a5
  • 国家/地区:%C
  • 国家/地区代码:%c
  • 时区:%T