Skip to content

Instantly share code, notes, and snippets.

@juancasantito
Created February 1, 2016 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juancasantito/ea6c2366db98a25b08e4 to your computer and use it in GitHub Desktop.
Save juancasantito/ea6c2366db98a25b08e4 to your computer and use it in GitHub Desktop.
$url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452';
$client = new \GuzzleHttp\Client;
$response = $client->get($url, [
'headers' => ['Accept' => application/json],
])->getBody();
$array = json_decode($response, TRUE);
$iterator = new \RecursiveIteratorIterator(
new \RecursiveArrayIterator($array),
\RecursiveIteratorIterator::SELF_FIRST);
// Recurse through the result array. When there is an array of items at the
// expected depth that has the expected identifier as one of the keys, pull that
// array out as a distinct item.
$identifier = 'place_id';
$identifierDepth = 1;
$items = [];
while ($iterator->valid()) {
$iterator->next();
$item = $iterator->current(); // Segfaults on last row from gmap data.
if (is_array($item)) {
if (array_key_exists($identifier, $item)) {
if ( $iterator->getDepth() == $identifierDepth) {
$items[] = $item;
}
}
}
}
echo 'no error';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment