Php 根据其他同级值更新多维数组值

Php 根据其他同级值更新多维数组值,php,arrays,recursion,Php,Arrays,Recursion,我有一个数组,它最多可以是n个元素 ( [children] => Array ( [0] => Array ( [id] => nhbs0123620cf897 [title] => Introduction [children] => Array (

我有一个数组,它最多可以是n个元素

    (
[children] => Array
    (
        [0] => Array
            (
                [id] => nhbs0123620cf897
                [title] => Introduction
                [children] => Array
                    (
                        [0] => Array
                            (
                                [id] => bylff0a76617c8
                                [title] => Courent3
                                [children] => Array
                                    (
                                    )
                            )

                        [1] => Array
                            (
                                [id] => xavs26efa2f51eb
                                [title] => Chapter
                                [children] => Array
                                    (
                                    )
                            )

                        [2] => Array
                            (
                                [id] => iezkd241d9d90
                                [title] => external
                                [children] => Array
                                    (
                                    )
                            )

                        [3] => Array
                            (
                                [id] => gmzh439c4f50
                                [title] => audio
                                [children] => Array
                                    (
                                    )
                            )

                        [4] => Array
                            (
                                [id] => niugd4e18b0
                                [title] => url
                                [children] => Array
                                    (
                                    )
                            )

                        [5] => Array
                            (
                                [id] => unpgdb1b7694
                                [title] => new
                                [children] => Array
                                    (
                                    )

                            )

                        [6] => Array
                            (
                                [id] => ssvc2025c0c8a
                                [title] => simple
                                [children] => Array
                                    (
                                    [0] => Array
                                        (
                                            [id] => ssvc2025c0c
                                            [title] => later
                                            [children] => Array
                                                (
                                                )
                                        )
                                    )
                            )

                    )

            )

        [1] => Array
            (
                [id] => rwtae5d9482
                [title] => Summary
                [children] => Array
                    (
                    [0] => Array
                            (
                                [id] => ssvc2025c0
                                [title] => later
                                [children] => Array
                                    (
                                    )
                            )
                    )

            )
        [2] => Array
            (
                [id] => rwtae5d9482709
                [title] => Course
                [children] => Array
                    (
                    )

            )

    )

  )
这里我想根据数组中每个元素的id更新标题值

我试过的

1使用数组\u walk\u递归在这里我可以更新数据,但在更新时无法检查id值

 array_walk_recursive(array, function(&$value, $key) {
      // not able to check if id == 'something' .this is what i need 
        if ($key == 'title') {
            $value = 'updated data';
        }
    });
第二次尝试用于每个循环,但无法保存数组索引以获取实际数组

 function myfun($array,&$out){
    foreach($array['children'] as $key=>$val){
        if(!empty($val['children'])){
            $out['children'][$key]['title'] = $val['title']; // this is not right key 
            $this->myfun($val,$out['children']);
        }else{
            $out['children'][$key]['title'] = $val['title'];  // this is not right key
        }
    }
}
这里也不能返回$out数组中的数组。 我唯一的一件事是,如果可以编写任何函数,那么使用这个键的子键


我希望php数组函数中可以实现这一点。

您已接近-您所需要的只是使用
&
-这将将数组作为引用发送,因此对其所做的任何更改都将位于原始数组上。可以寻找更多的知识

考虑以下几点:

$arr = [["id" => 1, "title" => "aaa", "children" => []], ["id" => 2, "title" => "bbb", "children" => [["id" => 3, "title" => "ccc", "children" => []]]]];

function modifyTitle(&$arr, $id, $newVal) {
    foreach($arr as &$e) {
        if ($e["id"] == $id)
            $e["title"] = $newVal;
        modifyTitle($e["children"], $id, $newVal);
    }

}
modifyTitle($arr, 3, "zzz");
print_r($arr); // now the original array children changed

实例:

谢谢,成功了!通过引用传递值是我为foreach错过的技巧,再次感谢