Searching Google Geocoder Information
Before I forget I just want to post this bit of code - took me way too long to figure this out - it’s a long story. In any case, if you’re ever using PHP to get google geocoder information and it’s stored in a JSON Object - you probably realized it’s hard to find a particular key at times because of varying levels of data returned by google… in any case, you can use this function to quickly and easily get the first result for a matching key.
function getPlaceValue($object, $lookingFor) {
foreach($object as $key=>$obj) {
if(strcmp($key,$lookingFor)==0) {
return $obj;
} else if(is_object($obj)) {
$ok = getPlaceValue($obj,$lookingFor);
if($ok) {
return $ok;
}
}
}
return false;
}
That should work for any nested object as well… so enjoy!
Posted in Uncategorized