php – 递归搜索并删除数组?

我正在使用多维数组,我希望能够删除与id匹配的数组(和所有子项).

我试过的功能是:

function removeKey($key, $array, $childKey = 'children'){
    if(isset($array[$key])){
        unset($array[$key]);
        return $array;
    }

    foreach($array as &$item)
        if(isset($item[$childKey]))
            $item = removeKey($key, $item[$childKey], $childKey);

    return $array;
}

我的示例数组是:

Array
(
    [5] => Array
        (
            [id] => 5
            [parent_id] => 
            [menu_title] => Drinks
            [page_title] => Drinks
            [status] => 1
            [products] => 0
        )

    [1] => Array
        (
            [id] => 1
            [parent_id] => 
            [menu_title] => Electronics
            [page_title] => Electronics
            [status] => 1
            [products] => 0
            [children] => Array
                (
                    [2] => Array
                        (
                            [id] => 2
                            [parent_id] => 1
                            [menu_title] => Computers
                            [page_title] => Computers
                            [status] => 1
                            [products] => 0
                            [children] => Array
                                (
                                    [4] => Array
                                        (
                                            [id] => 4
                                            [parent_id] => 2
                                            [menu_title] => Apple
                                            [page_title] => Apple - Imacs and Macbooks
                                            [status] => 1
                                            [products] => 0
                                        )

                                )

                        )

                    [3] => Array
                        (
                            [id] => 3
                            [parent_id] => 1
                            [menu_title] => Mobile Phones
                            [page_title] => Mobile Phones
                            [status] => 1
                            [products] => 0
                        )

                )

        )

)

我正在寻找的结果(用(2,$array,’children’)调用函数)是:

Array
(
    [5] => Array
        (
            [id] => 5
            [parent_id] => 
            [menu_title] => Drinks
            [page_title] => Drinks
            [status] => 1
            [products] => 0
        )

    [1] => Array
        (
            [id] => 1
            [parent_id] => 
            [menu_title] => Electronics
            [page_title] => Electronics
            [status] => 1
            [products] => 0
            [children] => Array
                (

                    [3] => Array
                        (
                            [id] => 3
                            [parent_id] => 1
                            [menu_title] => Mobile Phones
                            [page_title] => Mobile Phones
                            [status] => 1
                            [products] => 0
                        )

                )

        )

)

但我得到的结果是

Array
(
    [5] => Array
        (
            [id] => 5
            [parent_id] => 
            [menu_title] => Drinks
            [page_title] => Drinks
            [status] => 1
            [products] => 0
        )

    [1] => Array
        (
            [3] => Array
                (
                    [id] => 3
                    [parent_id] => 1
                    [menu_title] => Mobile Phones
                    [page_title] => Mobile Phones
                    [status] => 1
                    [products] => 0
                )

        )

)

我不知道这里发生了什么!

解决方法:

您只需使用引用即可轻松完成任务.

function removeKey($key, &$array, $childKey = 'children'){
    if(isset($array[$key])){
        unset($array[$key]);
        return;
    }

    foreach($array as &$item)
        if(isset($item[$childKey]))
            removeKey($key, $item[$childKey], $childKey);
}

例:

$arr = array(...);
removeKey('key', $arr, $chilKey);
// Just continue using $arr
上一篇:第三周刷题


下一篇:学习笔记215—主要的几个脑网络