递归搜索并删除数组?

时间:2012-08-21 11:00:55

标签: php arrays search recursion unset

我正在使用多维数组,我希望能够删除与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
                )

        )

)

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

2 个答案:

答案 0 :(得分:2)

您只需使用参考资料即可轻松完成任务。

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

答案 1 :(得分:0)

多维数组怎么样?我为这个解决方案研究了几个小时,没有找到最佳解决方案。所以,我自己写了

function allow_keys($arr, $keys)
    {
        $saved = [];

        foreach ($keys as $key => $value) {
            if (is_int($key) || is_int($value)) {
                $keysKey = $value;
            } else {
                $keysKey = $key;
            }
            if (isset($arr[$keysKey])) {

                $saved[$keysKey] = $arr[$keysKey];
                if (is_array($value)) {

                    $saved[$keysKey] = allow_keys($saved[$keysKey], $keys[$keysKey]);
                }
            }
        }
        return $saved;
    }

使用:示例

$array = [
        'key1' => 'kw',
        'loaa'=> ['looo'],
        'k'    => [
            'prope' => [
                'prop'  => ['proo', 'prot', 'loolooo', 'de'],
                'prop2' => ['hun' => 'lu'],
            ],
            'prop1' => [

            ],
        ],
    ];

致电:示例

allow_keys($array, ['key1', 'k' => ['prope' => ['prop' => [0, 1], 'prop2']]])

输出:

Array ( [key1] => kw [k] => Array ( [prope] => Array ( [prop] => Array ( [0] => proo [1] => prot ) [prop2] => Array ( [hun] => lu ) ) ) ) 

因此您只需从多维数组中获取所需的键。它不仅限于" multidimensional",你可以通过传递像

这样的数组来使用它
['key1', 'loaa']

输出你得到:

Array ( [key1] => kw [loaa] => Array ( [0] => looo ) )

希望有人帮助这个,因为我经常搜索,没有找到任何东西。 干杯!