I have the following array:
$array = array('23' => array('19' => array('7' => array('id' => 7, 'name' => 'John Doe'))));
Array
(
[23] => Array
(
[19] => Array
(
[7] => Array
(
[id] => 7
[name] => John Doe
)
)
)
)
I want to access sub-element and i know his sub keys that 23 19 7
. I can do this with simple format
echo $array['23']['19']['7']['name']; // John Doe
But these array have just 3 level and this may vary, more or less. I have to make an array unlimited level
.
I tried that like i want with following codes:
$keys = array('23', '19', '7');
echo $array[$keys]['name'];
And of course i got Warning: Illegal offset type in
error.
Then i tried this one but i couldnt get any element:
function brackets($str) {
return sprintf("['%s']", $str);
}
$keys = array('23', '19', '7');
$string_key = implode('', array_map('brackets', $keys)); // ['23']['19']['7']
echo $array{$string_key}['name'];