vendredi 30 août 2019

How to get country code from object in php?

I am beginner in php/laravel.

I have array with objects

This is sample object:

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
)

My php code:

foreach (app('geocoder')->geocode($request->input('city'))->get() as $key => $value) {
            //echo $value->country->code;
            echo"<pre>"; print_r($value); echo"</pre>";
        }

I want check country code. If country->code == PL - then I hand show text: "It's correct" or if not - then I want show text "wrong".

How can I make it?

I need result:

It's correct
It's correct
It's correct
wrong
It's correct

etc

How can I make it?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire