Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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

share|improve this question
    
I think the array format stays as is so above should work fine. Just an idea - Perhaps you need to retrieve the information as XML and use xpath. OR if you wish to stick with above maybe you need to create a function which loops through this data array and checks for key 'locality' etc – HappyApe Aug 3 '12 at 7:24
up vote 0 down vote accepted
    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[] = ....

share|improve this answer
    
That's great. If I wanted to add other variable to the array would the code be: $locality[] = $location[types][0]; AND $locality[] = $location['short_name'];? – Shaun Wright Aug 3 '12 at 8:57
    
You should do it like that: $locality[] = array( 'type' => $location['types'][0], 'short_name' => $location['short_name'] ); This way , each key in the array is a location. Access it like: $locality[X]['type'] and $locality[X]['short_name']. When x is the key number (from 0 to ...) – Ofir Baruch Aug 3 '12 at 9:01

You could do a preg_match with this pattern:

|"long_name" : "([^"]*)",\r\n"short_name" : "([^"]*)",\r\n"types" : \[ [^\]]*"locality"|

Demo

This will return an array with 3 elemnts with the long name at index 1 and the short name at index 2

OR use a loop:

foreach($phpresult['results'][0]['address_components'] as $component){
  if(array_search('location', $component['types']) !== false){
    $location = $component['long_name'];
    break;
  }
}

If this is to hardcoded for you, you can add another loop to check all results.

share|improve this answer
    
That's great. Thanks. I actually created a foreach loop and figured it out after I posted it but my code seems to server intensive. I have 3 foreach loops (nested: I think that's the right term) and an if statement. Which do you think would be the most efficient and less server intensive? Also, is one or the other considered best practice? I'm referencing your two above options as well as mine in this comment. – Shaun Wright Aug 3 '12 at 8:47
    
It depends on the size of the returned array. Regex is rather slow but parsing a LARGE json answer takes time too. I honestly do not know what is better in your case but I would go with the regex. – Oliver A. Aug 3 '12 at 8:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.