使用父级和子级结构 PHP 更改动态嵌套数组中的特定值

Change specific value in Dynamic nested array with parent and children structure PHP

提问人:Adarsh Das 提问时间:12/3/2022 更新时间:12/3/2022 访问量:87

问:

我有一个具有唯一 id 和名称的动态嵌套数组结构,需要更改给定特定 id 的 name 值。

我有一个带有结构的动态嵌套数组 `

$array = [
    [
        "id" => "31350880",
        "name" => "HOD",
        "children" => [
            [
                "id" => "57f94cd7",
                "parent_id" => "31350880",
                "name" => "New HOD",
                "children" => [
                    [
                        "id" => "e7f1c88b",
                        "parent_id" => "57f94cd7",
                        "name" => "Gold",
                        "children" => [
                        ]
                    ]
                ]
            ]
        ]
    ],
    [
        "id" => "45881fa8",
        "name" => "Pictures",
        "children" => [
            [
                "id" => "770e6e20",
                "parent_id" => "45881fa8",
                "name" => "New Picture",
                "children" => [
                    [
                        "id" => "a403a8fa",
                        "parent_id" => "770e6e20",
                        "name" => "Silver",
                        "children" => [
                        ]
                    ]
                ]
            ]
        ]
    ]
];

`

对于给定的 ID(unique),需要从数组中找到它,并将该节点的名称更改为特定名称。

例如: $id = ''770e6e20' 它是名为 “Pictures” 的父节点的子节点,需要找到具有特定 id 的子节点并更改其名称并检索其初始结构中的完整数组吗?

php 数组 json laravel 嵌套

评论

0赞 berend 12/3/2022
您可以设想使用递归函数在源数组之间循环,直到找到匹配项 ()。然后,您可以按照您认为合适的方式更改或操作数组。idname

答:

0赞 Vasilis G. 12/3/2022 #1

这是一个递归函数,它获取数组、项目和要分配的新值,并执行就地修改:referenceidname

function modify_element_name(&$arr, $id, $name){
    foreach($arr as $index => $item){
        if($item['id'] === $id){
            $arr[$index]['name'] = $name;
            
        }
        else {
            if(count($item['children']) > 0){
                modify_element_name($arr[$index]['children'], $id, $name);
            }
        }
    }
}

调用方式如下:

modify_element_name($array, '770e6e20', 'New Picture Modified');

将修改数组:

Array
(
    [0] => Array
        (
            [id] => 31350880
            [name] => HOD
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 57f94cd7
                            [parent_id] => 31350880
                            [name] => New HOD
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => e7f1c88b
                                            [parent_id] => 57f94cd7
                                            [name] => Gold
                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 45881fa8
            [name] => Pictures
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 770e6e20
                            [parent_id] => 45881fa8
                            [name] => New Picture Modified
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => a403a8fa
                                            [parent_id] => 770e6e20
                                            [name] => Silver
                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)