-2

So I have this data wich is returned from a request and then decoded with json_decode, I wish to access the city and postcode values from it. But my knowledge of php is very limited, and I can't seem to access them via -> operator or ['properties']. Am I doing something wrong, or is this supposed to be accessed via a for each? Or via a numerical index?

public function getGeoData($latitude, $longitude) {
    $lat = strval(number_format((float)$latitude, 5, '.', ''));
    $lon = strval(number_format((float)$longitude, 5, '.', ''));    
    $response = Http::get('https://nominatim.openstreetmap.org/reverse?format=geojson&lat=' . $lat . '&lon=' . $lon);
    return json_decode($response);
}

returned data:

        [
    {
        "type": "Feature",
        "properties": {
            "place_id": 27241982,
            "osm_type": "way",
            "osm_id": 103870558,
            "place_rank": 30,
            "category": "place",
            "type": "house",
            "importance": -0.1,
            "addresstype": "place",
            "name": null,
            "display_name": "543, Rue de la Reine, District03, Saint-Roch, La Cité-Limoilou, Québec, Québec (Agglomération), Capitale-Nationale, Québec, G1K 6A2, Canada",
            "address": {
                "house_number": "543",
                "road": "Rue de la Reine",
                "residential": "District03",
                "suburb": "Saint-Roch",
                "city_district": "La Cité-Limoilou",
                "city": "Québec",
                "county": "Québec (Agglomération)",
                "region": "Capitale-Nationale",
                "state": "Québec",
                "ISO3166-2-lvl4": "CA-QC",
                "postcode": "G1K 6A2",
                "country": "Canada",
                "country_code": "ca"
            }
        },
        "bbox": [
            -71.223775242105,
            46.816099889474,
            -71.223675242105,
            46.816199889474
        ],
        "geometry": {
            "type": "Point",
            "coordinates": [
                -71.22372524210526,
                46.816149889473685
            ]
        }
    }
]
  • If the above link doesn't help, post your code/attempt, expected result and what currently happens (we need details, incl. full error messages, if any.) Also read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and [how to ask](https://stackoverflow.com/help/how-to-ask). – M. Eriksson Apr 15 '22 at 13:58

1 Answers1

0

I just realised what I was doing wrong, it seems that my data is encapsulated inside an array first.

All I had to do was $data[0]->properties->adress->city