3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getValueRecursive(array $array, ...$keys) { foreach ($keys as $key) { if (!is_array($array)) { throw new Exception('non-array entity in key path'); } if (!key_exists($key, $array)) { throw new Exception('key path invalid'); } $array = $array[$key]; } return $array; } $foo = [ 'a' => [ 'b' => [ 'c' => "Hallo Welt!" ] ] ]; try { var_export(getValueRecursive($foo, 'a', 'b', 'c', 0)); } catch (Exception $e) { echo 'Caught exception: ' . $e->getMessage(); }

preferences:
129.2 ms | 405 KiB | 5 Q