递归搜索和删除数组?(Recursive search and remove in array?)

2019-06-27 04:48发布

我有一个多维数组我希望能够删除阵列(和所有儿童)匹配的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
                )

        )

)

我不知道这是怎么回事就在这里!

Answer 1:

你可以只用引用缓解东西。

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


Answer 2:

什么多维数组? 我研究了该解决方案几个小时,无处找到了一个最佳的解决方案。 所以,我自己写的

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 ) ) ) ) 

所以你得到的只是需要从多维数组的键。 它不仅限于对“多维”,你可以通过一个数组一样使用它

['key1', 'loaa']

输出你:

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

希望有人可以帮助这一个,因为我搜索了很多,并没有什么发现。 干杯!



文章来源: Recursive search and remove in array?