Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google geocode. Parsing JSON issue

Tags:

json

php

geocode

I want to get a users city, state/province, country from a google geocode of their postal/zip code (canada/usa). I retrieve the data as JSON: https://developers.google.com/maps/documentation/geocoding/#JSON

The problem is that there is not always the same amount/order of 'address_components'. I was using just like: $geocodedinfo['results'][0]['address_components'][3]['short_name']; But I soon realized that that will not always be province/state because sometimes google will add an extra element under 'address_components'.

Is there a way to parse the results just for the specifics I need (ie: City, State/Province, Country)?

EDIT I actually decode the JSON when I get it json_decode($result, true)

Array
(
[results] => Array
    (
        [0] => Array
            (
                [address_components] => Array
                    (
                        [0] => Array
                            (
                                [long_name] => V2X 2P6
                                [short_name] => V2X 2P6
                                [types] => Array
                                    (
                                        [0] => postal_code
                                    )

                            )

                        [1] => Array
                            (
                                [long_name] => Maple Ridge
                                [short_name] => Maple Ridge
                                [types] => Array
                                    (
                                        [0] => locality
                                        [1] => political
                                    )

                            )

                        [2] => Array
                            (
                                [long_name] => Maple Ridge
                                [short_name] => Maple Ridge
                                [types] => Array
                                    (
                                        [0] => administrative_area_level_3
                                        [1] => political
                                    )

                            )

                        [3] => Array
                            (
                                [long_name] => Greater Vancouver Regional District
                                [short_name] => Greater Vancouver Regional District
                                [types] => Array
                                    (
                                        [0] => administrative_area_level_2
                                        [1] => political
                                    )

                            )

                        [4] => Array
                            (
                                [long_name] => British Columbia
                                [short_name] => BC
                                [types] => Array
                                    (
                                        [0] => administrative_area_level_1
                                        [1] => political
                                    )

                            )

                        [5] => Array
                            (
                                [long_name] => Canada
                                [short_name] => CA
                                [types] => Array
                                    (
                                        [0] => country
                                        [1] => political
                                    )

                            )

                    )

                [formatted_address] => Maple Ridge, BC V2X 2P6, Canada
                [geometry] => Array
                    (
                        [bounds] => Array
                            (
                                [northeast] => Array
                                    (
                                        [lat] => 49.2214351
                                        [lng] => -122.6577849
                                    )

                                [southwest] => Array
                                    (
                                        [lat] => 49.219268
                                        [lng] => -122.663613
                                    )

                            )

                        [location] => Array
                            (
                                [lat] => 49.2202679
                                [lng] => -122.660587
                            )

                        [location_type] => APPROXIMATE
                        [viewport] => Array
                            (
                                [northeast] => Array
                                    (
                                        [lat] => 49.2217005303
                                        [lng] => -122.6577849
                                    )

                                [southwest] => Array
                                    (
                                        [lat] => 49.2190025697
                                        [lng] => -122.663613
                                    )

                            )

                    )

                [types] => Array
                    (
                        [0] => postal_code
                    )

            )

    )

[status] => OK
)
like image 950
cantaffordavan Avatar asked Jan 24 '26 21:01

cantaffordavan


1 Answers

You're going to have to loop over the results.
Most of the relevant PHP functions don't support multidimensional arrays, and the ones that do will be looping anyway.

You'll probably want something like this:

foreach ($geocodedinfo["results"] as $result) {
    foreach ($result["address_components"] as $address) {
        // Repeat the following for each desired type
        if (in_array("country", $address["types"])) {
            $country = $address["long_name"];
        }
    }
}
like image 161
Sara Avatar answered Jan 27 '26 09:01

Sara