Shaun Wright Shaun Wright - 7 months ago 5
SQL Question

parse json data from google maps html call to mysql database using php

I want to add the locality name from a google maps html call(below) to a specific php variable so that I can 1) store it in a mysql database and 2) display it as part of an echo statement (echo $locality).


http://maps.googleapis.com/maps/api/geocode/json?latlng=34.11375320612877,-118.35220470764159&sensor=false


I have decoded the json data via: json_decode($data,true); statement.

Given the complexity of the multivariable array structure, I'd like to dive down and pull out a specific value if it exists. An example to help explain.

Here is the json data abreviated to save space:


{
"results" : [

{

"address_components" : [

{

"long_name" : "2917-2981",

"short_name" : "2917-2981",

"types" : [ "street_number" ]

},

{
"long_name" : "Runyon Canyon Rd",

"short_name" : "Runyon Canyon Rd",

"types" : [ "route" ]

},

{

"long_name" : "Hollywood Hills",

"short_name" : "Hollywood Hills",

"types" : [ "neighborhood", "political" ]

},

{

"long_name" : "Los Angeles",

"short_name" : "Los Angeles",

"types" : [ "locality", "political" ]

}

....


my code:


...

$data = @file_get_contents($url);

$phpresult = json_decode($data,true);

var_dump($phpresult);



var_dump($phpresult) displays:


array(2) { ["results"]=> array(8) { [0]=> array(4) { ["address_components"]=> array(8) { [0]=> array(3) { ["long_name"]=> string(9) "2917-2981" ["short_name"]=> string(9) "2917-2981" ["types"]=> array(1) { [0]=> string(13) "street_number" } } [1]=> array(3) { ["long_name"]=> string(16) "Runyon Canyon Rd" ["short_name"]=> string(16) "Runyon Canyon Rd" ["types"]=> array(1) { [0]=> string(5) "route" } } [2]=> array(3) { ["long_name"]=> string(15) "Hollywood Hills" ["short_name"]=> string(15) "Hollywood Hills" ["types"]=> array(2) { [0]=> string(12) "neighborhood" [1]=> string(9) "political" } } [3]=> array(3) { ["long_name"]=> string(11) "Los Angeles" ["short_name"]=> string(11) "Los Angeles" ["types"]=> array(2) { [0]=> string(8) "locality" [1]=> string(9) "political" }...<


I'd like to store the value "Los Angeles" in variable $locality by searching/parsing/? the array for the condition "type = locality".

I do not want find the information by using the code:


echo $phpresult['results'][0]['address_components'][3]['long_name'];


because that assumes the variable is always 1) in the same location within the array and 2) that it exists.

Any help would be really appreciated. I haven't found an answer and have probably read (but not totally understood) every post relating to the subject.

Thanks in advance for your help.

Shaun

Answer
foreach($phpresult['results'][0]['address_components'] as $location)
{
    if(in_array("locality" , $location['types']))
    {
        $locality[] = $location['long_name'];
    }
}

print_r($locality);

The array of each location has 3 keys: long_name , short_name and types. Types is an array , so , if there's a value of "locality" in that types array get the long_name of that location and store it.

EDIT: This function will store all the long_names of location which has a locality type, not just the first one. In order to get only the first location's long_name ,add "break" after the $locality[] = ....

Comments