PHP:按索引查找、存储、地址数组键


PHP: Find, store, address array key by index

我有一个非常复杂的多维数组($tree)。我收到那个大数组作为参考。

现在,我需要在其中找到某个键并在那里插入数据。

找到所需的密钥很容易。函数搜索数组并返回路径$path。例如,它返回$path = array('index1', 'index2', 'index3') 。这意味着,我需要像$tree['index1']['index2']['index3'] = $some_data_i_needed_to_insert一样分配我的数据。

现在出现的问题是我无法从从 seatch 函数收到的地址寻址该数组索引。

我试过这样:

<?php
$path = '[''index1''][''index2''][''index3'']';
$tree{$path} = $some_data_i_needed_to_insert;
?>

在我的情况下,有没有办法解决数组索引?

如果您有路径数组,则没有理智的直接表达式可用于直接访问密钥。但是,这将执行:

$path =  array('1334', '#below', '3242');
$node =& $complexArray;
foreach ($path as $key) {
    $node =& $node[$key];
}
$node = $data;