Created
February 1, 2016 20:23
-
-
Save juancasantito/ea6c2366db98a25b08e4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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