PHP在递归搜索期间将值推送到数组

时间:2016-05-12 13:07:08

标签: php arrays recursion

    $menus = [
        0 => [
            'id' => 'home',
            'title' => 'Home',
            'url' => '/display/home',
            'children' => [],
            'parent' => null
        ],
        1 => [
            'id' => 'nodes',
            'title' => 'Nodes',
            'url' => 'nodes/index',
            'children' => [
                0 => [
                    'id' => 'addNode',
                    'title' => 'Add Node',
                    'url' => '/nodes/add',
                    'children' => [],
                    'parent' => "nodes"
                ],
                1 => [
                    'id' => 'editNode',
                    'title' => 'Edit Node',
                    'url' => '/nodes/edit',
                    'children' => [],
                    'parent' => 'nodes'
                ],
                2 => [
                    'id' => 'deleteNode',
                    'title' => 'Delete Node',
                    'url' => '/nodes/delete',
                    'children' => [
                        0 => [
                            'id' => 'deleteMultipleNodes',
                            'title' => 'Delete Multiple Nodes',
                            'url' => '/nodes/deleteall',
                            'children' => [
                                0 => [
                                    'id' => 'deleteMultipleSelectedNodes',
                                    'title' => 'Delete Multiple Selected Nodes',
                                    'url' => '/nodes/deleteallselected',
                                    'children' => [],
                                    'parent' => 'deleteMultipleNodes'
                                ]
                            ],
                            'parent' => 'deleteNode'
                        ]
                    ],
                    'parent' => 'nodes'
                ]

            ],
            'parent' => null
        ]
    ];

假设我有这个数组。我想要的是以递归方式搜索此数组中的“id”,如果找到则将新子项推送到该元素的children数组中。

我通过不同的方式尝试过,我也尝试使用 RecursiveArrayIterator 来遍历数组,但问题是如何在找到时将值推送到数组的索引遍历。

例如,这里是我尝试的代码之一:

private function traverseArray($array)
{

    $child = [
        'id' => 'deleteMultipleNotSelectedNodes',
        'title' => 'Delete Multiple Not Selected Nodes',
        'url' => '/nodes/deletenotselected',
        'children' => [],
        'parent' => 'deleteMultipleNodes'
    ];
    foreach($array as $key=>$value)
    {
        if(is_array($value))
        {
            $this->traverseArray($value);
        }
        if($key == "id" && $value == "deleteMultipleNodes") 
        {
            array_push($array["children"], $child); // This part is confusing me, How to add the child on this index where the id is found. 
        }
    }
}

任何有关如何以有效方式进行此类操作的帮助都可以节省我的时间。

2 个答案:

答案 0 :(得分:3)

如果没有使用$ this并修复错误来比较$ value而不是将任何值分配给它,它将如何工作。

请注意与& $ array和& $值的区别,它们是引用,因此它会更改原始数据,而不是将其复制到新变量中。

<?php
$menus = [
        0 => [
            'id' => 'home',
            'title' => 'Home',
            'url' => '/display/home',
            'children' => [],
            'parent' => null
        ],
        1 => [
            'id' => 'nodes',
            'title' => 'Nodes',
            'url' => 'nodes/index',
            'children' => [
                0 => [
                    'id' => 'addNode',
                    'title' => 'Add Node',
                    'url' => '/nodes/add',
                    'children' => [],
                    'parent' => "nodes"
                ],
                1 => [
                    'id' => 'editNode',
                    'title' => 'Edit Node',
                    'url' => '/nodes/edit',
                    'children' => [],
                    'parent' => 'nodes'
                ],
                2 => [
                    'id' => 'deleteNode',
                    'title' => 'Delete Node',
                    'url' => '/nodes/delete',
                    'children' => [
                        0 => [
                            'id' => 'deleteMultipleNodes',
                            'title' => 'Delete Multiple Nodes',
                            'url' => '/nodes/deleteall',
                            'children' => [
                                0 => [
                                    'id' => 'deleteMultipleSelectedNodes',
                                    'title' => 'Delete Multiple Selected Nodes',
                                    'url' => '/nodes/deleteallselected',
                                    'children' => [],
                                    'parent' => 'deleteMultipleNodes'
                                ]
                            ],
                            'parent' => 'deleteNode'
                        ]
                    ],
                    'parent' => 'nodes'
                ]

            ],
            'parent' => null
        ]
    ];

function traverseArray(&$array)
{
    $child = [
        'id' => 'deleteMultipleNotSelectedNodes',
        'title' => 'Delete Multiple Not Selected Nodes',
        'url' => '/nodes/deletenotselected',
        'children' => [],
        'parent' => 'deleteMultipleNodes'
    ];
    foreach($array as $key=>&$value)
    {
        if(is_array($value))
        {
            traverseArray($value);
        }
        if($key == "id" && $value == "deleteMultipleNodes") 
        {
            array_push($array["children"], $child); 
        }
    }
}

echo "=== before \n";
var_export($menus);
echo "\n\n";
traverseArray($menus);
echo "=== after \n";
var_export($menus);

答案 1 :(得分:0)

也许这会对你有所帮助。 array_walk_recursive将遍历所有元素。并且回调函数可以通过引用接收$value。因此,当您修改$value时,这将修改源数组中的项目。

array_walk_recursive($array, function (&$value, $key) {
    if(is_array($value) && $value['id'] === "deleteMultipleNodes") {
        $child = [
            'id' => 'deleteMultipleNotSelectedNodes',
            'title' => 'Delete Multiple Not Selected Nodes',
            'url' => '/nodes/deletenotselected',
            'children' => [],
            'parent' => 'deleteMultipleNodes'
        ];
        array_push($value['children'], $child);
    }
});