访问多维数组中的任意深度键

Bra*_*ldt 3 php arrays multidimensional-array

如果我有一个包含的数组['key1', 'key2', 'key3']有没有办法将它映射到数组$array['key1']['key2']['key3']而不使用循环或eval()?

数组示例:

$var = [
    'key1' => [
        'subkey1' => [
            'finalkey' => 'value',
        ],
        'subkey' => [
            'otherkey' => 'value',
        ],
    ],
    'key2' => 'blah'
];
Run Code Online (Sandbox Code Playgroud)

然后我有一个像这样的数组:

$keys = ['key1', 'subkey1', 'finalkey'] 
Run Code Online (Sandbox Code Playgroud)

要么

$keys = ['key1', 'subkey']
Run Code Online (Sandbox Code Playgroud)

Tim*_*per 5

function array_find($needle, &$haystack)
{
    $current = array_shift($needle);
    if(!isset($haystack[$current]))
    {
        return null;
    }
    if(!is_array($haystack[$current]))
    {
        return $haystack[$current];
    }
    return array_find($needle, $haystack[$current]);
}
Run Code Online (Sandbox Code Playgroud)