Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当我只知道子元素的位置时,尝试更新多维关联数组的值_Php - Fatal编程技术网

Php 当我只知道子元素的位置时,尝试更新多维关联数组的值

Php 当我只知道子元素的位置时,尝试更新多维关联数组的值,php,Php,我正在为多维关联数组运行递归函数。我知道我想做什么,但我无法找到正确的实现方法。假设我有这样一个数组: $my_array = array( '1' => array( 'id' => '1', 'parent' => false, 'children' => array( '2' => array( 'id' => '2',

我正在为多维关联数组运行递归函数。我知道我想做什么,但我无法找到正确的实现方法。假设我有这样一个数组:

$my_array = array(
    '1' => array(
        'id' => '1',
        'parent' => false,
        'children' => array(
            '2' => array(
                'id' => '2',
                'parent' => '1',
                'children' => array(
                    '3' => array(
                        'id' => '3',
                        'parent' => '2',
                        'children' => array(
                            //how do I insert an array here if I only have only have the location '3' to work from?           
                        )    
                    )    
                )    
            )
       )
    )   
);
我想在项目3的子项中插入一个新的项目数组,但我只有3作为位置。我如何才能达到这样一种情况,我可以做一些事情,比如:

$my_array['1']['children']['2']['children']['3']['children'] = $my_array;
如果我只使用“3”作为工作位置,而我不知道“3”之前的路径是什么


我考虑将数组转换为json,然后搜索'id'=>'3',然后尝试在children部分下将数组作为json注入,但问题是我无法找到如何找到json的右括号,因为在第3项的现有子项中可能还有其他右括号。

您可以
foreach()
覆盖数据,并在遇到带有元素的元素时使用递归函数
子元素
-因此将
子元素
传递回函数,直到找到后面的id为止。@NigelRen也许,我希望有更简单的解决方案,因为我已经有了递归函数,因此,我不太喜欢让递归函数执行另一个递归函数。您可以在数据上使用
foreach()
,当您遇到一个元素时使用递归函数,该元素有一个
子元素
-因此,将
子元素
传递回该函数,直到您找到后面的id为止。@NigelRen,我希望有更简单的解决方案,因为我已经有了一个递归函数,所以我不太喜欢让一个递归函数做另一个递归函数。