3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getValue($haystack, $indexes) { $indices = explode(',', $indexes); $finalIndex = array_pop($indices); $keys = []; foreach ($indices as $keys[]) { $keys[] = 'children'; } array_push($keys, $finalIndex, 'country', 'city'); //var_export($keys); foreach ($keys as $level => $key) { if (!key_exists($key, $haystack)) { throw new Exception( sprintf( "Path attempt failed for [%s]. No `%s` key found on levelIndex %d", implode('][', $keys), $key, $level ) ); } $haystack = $haystack[$key]; } return $haystack; } $test = [ [ 'children' => [ [ 'children' => [ [ 'country' => [ 'city' => 'Paris', ] ], [ 'country' => [ 'city' => 'Kyiv', ] ] ] ] ] ], [ 'children' => [ [ 'country' => [ 'city' => 'New York', ] ], [ 'country' => [ 'city' => 'Sydney', ] ] ] ] ]; $result = []; try { $result['0,0,0'] = getValue($test, '0,0,0'); $result['1,0'] = getValue($test, '1,0'); $result['1,0,0'] = getValue($test, '1,0,0'); } catch (Exception $e) { echo $e->getMessage() . "\n---\n"; } var_export($result);
Output for git.master, git.master_jit, rfc.property-hooks
Path attempt failed for [1][children][0][children][0][country][city]. No `children` key found on levelIndex 3 --- array ( '0,0,0' => 'Paris', '1,0' => 'New York', )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
84.77 ms | 405 KiB | 5 Q