3v4l.org

run code in 300+ PHP versions simultaneously
<?php function get_array_deep_value(array $array, array $levelKeys) { foreach ($levelKeys as $index => $key) { if (!key_exists($key, $array)) { throw new Exception("key $key not found at level index $index"); } $array = $array[$key]; } return $array; } $myArray = [ 'one' => [ 'two' => [ 'three' => [ 4 ] ] ] ]; $pathArrays = [ ['one', 'two', 'three'], ['one', 'two'], ['one'], ['two'] ]; foreach ($pathArrays as $pathArray) { try { var_export(get_array_deep_value($myArray, $pathArray)); } catch (Exception$e) { echo $e->getMessage(); } echo "\n---\n"; }

preferences:
23.27 ms | 405 KiB | 5 Q