递归搜索,并在数组中删除? [英] Recursive search and remove in array?

查看:115
本文介绍了递归搜索,并在数组中删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多维数组工作,我希望能够删除一个数组(和所有儿童)匹配一个id其中。

I am working with a multidimensional array I want to be able to remove an array (and all children) which match an 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;
}

我的例子数组是:

My example array is:

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,$阵列,'孩子'))是:

and the result I am looking for (calling the function with (2, $array, 'children')) is:

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
                        )

                )

        )

)

但我得到的结果是

but the result I am getting is

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
                )

        )

)

我不知道是怎么回事!

I have no idea what's going on here!

推荐答案

您可以只使用引用缓解的东西。

You can ease things by only using references.

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

这篇关于递归搜索,并在数组中删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆