Search code examples
phpgoogle-geocoder

Parse JSON array


I am using the google geocode api and it creates an array that I am not sure how to use. I need to pull specific elements from this array. Particularly the last latitude and longitude that is in the array.

{
    "results": [
        {
            "address_components": [
                {
                    "long_name": "1600",
                    "short_name": "1600",
                    "types": [
                        "street_number"
                    ]
                },
                {
                    "long_name": "President's Park",
                    "short_name": "President's Park",
                    "types": [
                        "establishment"
                    ]
                },
                {
                    "long_name": "Pennsylvania Avenue Northwest",
                    "short_name": "Pennsylvania Ave NW",
                    "types": [
                        "route"
                    ]
                },
                {
                    "long_name": "Washington",
                    "short_name": "Washington",
                    "types": [
                        "locality",
                        "political"
                    ]
                },
                {
                    "long_name": "District of Columbia",
                    "short_name": "DC",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "United States",
                    "short_name": "US",
                    "types": [
                        "country",
                        "political"
                    ]
                },
                {
                    "long_name": "20500",
                    "short_name": "20500",
                    "types": [
                        "postal_code"
                    ]
                }
            ],
            "formatted_address": "1600 Pennsylvania Avenue Northwest, President's Park, Washington, DC 20500, USA",
            "geometry": {
                "location": {
                    "lat": 38.8978378,
                    "lng": -77.0365123
                },
                "location_type": "ROOFTOP",
                "viewport": {
                    "northeast": {
                        "lat": 38.89918678029149,
                        "lng": -77.03516331970849
                    },
                    "southwest": {
                        "lat": 38.89648881970849,
                        "lng": -77.03786128029151
                    }
                }
            },
            "types": [
                "street_address"
            ]
        }
    ],
    "status": "OK"
}


Array
(
    [latitude] => -77.0365123
    [longitude] => 38.8978378
    [location_type] => ROOFTOP
)

Solution

  • Edit

    Since you have results in JSON and the structure is clear now with more formatting, here is how you can get to lat or lng value in that result

    $object=json_decode($json);
    $lat=$object->results[0]->geometry->location->lat; // same style for lng
    echo $lat;
    

    See Fiddle Here