0

This seems like such a rookie question but I'm just banging my head against the keyboard here and I can't find anything answered already that gets me moving forward.

Scenario is I'm trying to get the Lat/Lng of a zip code by geocoding it with Google Maps API. I've gotten the results back from Google Maps API as a JSON string and I've used json_decode to put it into a PHP array. But it looks likt it's an array of objects and i'm stumped on how I can drill down into the data to get my lat/lng value.

Here is the current road block... code then results:

<?php
$jsonData = '{"results":[{"address_components":[{"long_name":"33647","short_name":"33647","types":["postal_code"]},{"long_name":"Tampa","short_name":"Tampa","types":["locality","political"]},{"long_name":"Florida","short_name":"FL","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"Tampa, FL 33647, USA","geometry":{"bounds":{"northeast":{"lat":28.17150,"lng":-82.26235779999999},"southwest":{"lat":28.07291710,"lng":-82.42569910}},"location":{"lat":28.14343180,"lng":-82.33433749999999},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":28.17150,"lng":-82.26235779999999},"southwest":{"lat":28.07291710,"lng":-82.42569910}}},"types":["postal_code"]}],"status":"OK"}';
$phpArray = json_decode($jsonData);
print_r($phpArray);

foreach ($phpArray as $key => $value) { 
    echo "<p>$key | $value</p>";
}
?>

Results:

stdClass Object ( [results] => Array ( [0] => stdClass Object ( [address_components] => Array ( [0] => stdClass Object ( [long_name] => 33647 [short_name] => 33647 [types] => Array ( [0] => postal_code ) ) [1] => stdClass Object ( [long_name] => Tampa [short_name] => Tampa [types] => Array ( [0] => locality [1] => political ) ) [2] => stdClass Object ( [long_name] => Florida [short_name] => FL [types] => Array ( [0] => administrative_area_level_1 [1] => political ) ) [3] => stdClass Object ( [long_name] => United States [short_name] => US [types] => Array ( [0] => country [1] => political ) ) ) [formatted_address] => Tampa, FL 33647, USA [geometry] => stdClass Object ( [bounds] => stdClass Object ( [northeast] => stdClass Object ( [lat] => 28.1715 [lng] => -82.2623578 ) [southwest] => stdClass Object ( [lat] => 28.0729171 [lng] => -82.4256991 ) ) [location] => stdClass Object ( [lat] => 28.1434318 [lng] => -82.3343375 ) [location_type] => APPROXIMATE [viewport] => stdClass Object ( [northeast] => stdClass Object ( [lat] => 28.1715 [lng] => -82.2623578 ) [southwest] => stdClass Object ( [lat] => 28.0729171 [lng] => -82.4256991 ) ) ) [types] => Array ( [0] => postal_code ) ) ) [status] => OK )

results | Array

status | OK

URL used to create the input JSON:

http://maps.googleapis.com/maps/api/geocode/json?address=33647&sensor=false

Looking for some help to pull out the Lat and Long values out into a PHP variable.

Thanks in advance! Josh

1 Answer 1

1

The value of results is in fact another array - so you need to dig into the array to get the values you need.

This page (http://json.parser.online.fr/) might help you to visualize the data a bit more clearly.

Here's a terrible example with your data to demonstrate the depth (arrays as values):

<?php
$jsonData = '{"results":[{"address_components":[{"long_name":"33647","short_name":"33647","types":["postal_code"]},{"long_name":"Tampa","short_name":"Tampa","types":["locality","political"]},{"long_name":"Florida","short_name":"FL","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"Tampa, FL 33647, USA","geometry":{"bounds":{"northeast":{"lat":28.17150,"lng":-82.26235779999999},"southwest":{"lat":28.07291710,"lng":-82.42569910}},"location":{"lat":28.14343180,"lng":-82.33433749999999},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":28.17150,"lng":-82.26235779999999},"southwest":{"lat":28.07291710,"lng":-82.42569910}}},"types":["postal_code"]}],"status":"OK"}';
$phpArray = json_decode($jsonData,true);
print_r($phpArray);

foreach ($phpArray as $key => $value) {
    if ( $key == "results") {
        foreach ($value as $key2 => $value2) {
            foreach ($value2 as $key3 => $value3) {
                echo "<p>$key3 | $value3</p>";
            }
        }
    }
}
?>

You'll need to dig down a couple of levels to find the ll data you want. This should point you in the right direction though.

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

1 Comment

Thanks Russ... this really did get me on the right path. As another option, I dug a little deeper and found this which helped me dig down into the array or arrays :-) $fullurl = "maps.googleapis.com/maps/api/geocode/…"; $string = file_get_contents($fullurl); // get json content $json_a = json_decode($string, true); //json decoder echo $json_a['results'][0]['geometry']['location']['lat']; echo $json_a['results'][0]['geometry']['location']['lng'];

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.