Forked from juancasantito/RecursiveIteratorIterator fatal error
Last active
February 3, 2016 14:40
-
-
Save heddn/c6734630bf7bd9112423 to your computer and use it in GitHub Desktop.
This file contains 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()) { | |
$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; | |
} | |
} | |
} | |
$iterator->next(); | |
} | |
echo 'no error'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment