- var_export: documentation ( source)
- key_exists: documentation ( source)
<?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();
}