0

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
)
2
  • Loop through it and pick what you need? If this print_r output was formatted better it would surely have helped getting some answers Commented Oct 10, 2013 at 5:15
  • Sorry, I didnt realize it needed to be formatted. That makes sense why it looked so weird. How would I output just latitude and just longitude into new variables? Commented Oct 10, 2013 at 5:26

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

I put back the other part and left it as a paragraph how it is output
ok do one thing please, before you output that print_r in your code, add a <pre> tag and then print_r that, that will result in the output being formatted correctly. Post that one in your question
That is with the pre tag. When I use the code wrap it makes it one line
I know its a JSON array but I have no idea how to get the elements out of it that it displays in the end when I use the pre tags
can you post its JSON output? like the string as it is, before decoding it? Sorry as much as I want to help this formatting isn't letting me to
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.