<?php
function getValueRecursive(array $array, ...$keys) {
foreach ($keys as $key) {
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'));
echo "\n---\n";
var_export(getValueRecursive($foo, 'a', 'b'));
echo "\n---\n";
var_export(getValueRecursive($foo, 0, 'b', 'c'));
} catch (Exception $e) {
echo 'Caught exception: ' . $e->getMessage();
}
preferences:
31.31 ms | 405 KiB | 5 Q