- var_export: documentation ( source)
- key_exists: documentation ( source)
<?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();
var_export(null);
}
echo "\n---\n";
}