4

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
                )

        )

)

ここで何が起こっているのかわかりません!

4

2 に答える 2

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
于 2012-08-21T11:06:39.457 に答える