如果我有一个包含以下内容的数组['key1', 'key2', 'key3']
有什么方法可以将其映射到数组$array['key1']['key2']['key3']
不使用循环或 eval()?
数组示例:
$var = [
'key1' => [
'subkey1' => [
'finalkey' => 'value',
],
'subkey' => [
'otherkey' => 'value',
],
],
'key2' => 'blah'
];
然后我有一个像这样的数组:
$keys = ['key1', 'subkey1', 'finalkey']
or
$keys = ['key1', 'subkey']
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]);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)