简体   繁体   English

在多维数组中替换子数组中的父元素值?

[英]In multidimensional array replace parent element value from child arrays?

In the below array the most deepest array Sub Belt has 4 items.在下面的数组中,最深的数组带有 4 个项目。 I want to add this 4 items to it's parent category which is Belts , so the count value of belts should be 6.我想将这 4 个项目添加到它的父类别Belts 中,所以皮带的计数值应该是 6。

Now Hats , Belts have 2, 6 items accordingly and the parent category is Accessories .现在Hats , Belts有 2, 6 个相应的物品,父类别是Accessories So the count value of Accessories should be 8.所以附件的计数值应该是8。

Tshirts , Hoodies , Accessories have 5, 3, 8 items accordingly. Tshirts , Hoodies , Accessories有 5, 3, 8 项。 So the value of count element of parent category Clothing should be 16.所以父类Clothing的count元素的值应该是16。

Array
(
    [16] => Array
        (
            [id] => 16
            [name] => Clothing
            [count] => 0
            [parent_id] => 0
            [children] => Array
                (
                    [17] => Array
                        (
                            [id] => 17
                            [name] => Tshirts
                            [count] => 5
                            [parent_id] => 16
                        )

                    [18] => Array
                        (
                            [id] => 18
                            [name] => Hoodies
                            [count] => 3
                            [parent_id] => 16
                        )

                    [19] => Array
                        (
                            [id] => 19
                            [name] => Accessories
                            [count] => 0
                            [parent_id] => 16
                            [children] => Array
                                (
                                    [31] => Array
                                        (
                                            [id] => 31
                                            [name] => Hats
                                            [count] => 2
                                            [parent_id] => 19
                                        )

                                    [32] => Array
                                        (
                                            [id] => 32
                                            [name] => Belts
                                            [count] => 2
                                            [parent_id] => 19
                                            [children] => Array
                                                (
                                                    [36] => Array
                                                        (
                                                            [id] => 36
                                                            [name] => Sub Belt
                                                            [count] => 4
                                                            [parent_id] => 32
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [20] => Array
        (
            [id] => 20
            [name] => Music
            [count] => 2
            [parent_id] => 0
        )

    [21] => Array
        (
            [id] => 21
            [name] => Decor
            [count] => 3
            [parent_id] => 0
        )

)

The resulting array should be like this:结果数组应该是这样的:

Array
(
    [16] => Array
        (
            [id] => 16
            [name] => Clothing
            [count] => 16
            [parent_id] => 0
            [children] => Array
                (
                    [17] => Array
                        (
                            [id] => 17
                            [name] => Tshirts
                            [count] => 5
                            [parent_id] => 16
                        )

                    [18] => Array
                        (
                            [id] => 18
                            [name] => Hoodies
                            [count] => 3
                            [parent_id] => 16
                        )

                    [19] => Array
                        (
                            [id] => 19
                            [name] => Accessories
                            [count] => 8
                            [parent_id] => 16
                            [children] => Array
                                (
                                    [31] => Array
                                        (
                                            [id] => 31
                                            [name] => Hats
                                            [count] => 2
                                            [parent_id] => 19
                                        )

                                    [32] => Array
                                        (
                                            [id] => 32
                                            [name] => Belts
                                            [count] => 6
                                            [parent_id] => 19
                                            [children] => Array
                                                (
                                                    [36] => Array
                                                        (
                                                            [id] => 36
                                                            [name] => Sub Item
                                                            [count] => 4
                                                            [parent_id] => 32
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [20] => Array
        (
            [id] => 20
            [name] => Music
            [count] => 2
            [parent_id] => 0
        )

    [21] => Array
        (
            [id] => 21
            [name] => Decor
            [count] => 3
            [parent_id] => 0
        )

)

I got an idea from here, http://php.net/manual/en/class.recursiveiteratoriterator.php#120131我从这里得到了一个想法, http://php.net/manual/en/class.recursiveiteratoriterator.php#120131

RecusiveIteratorIterator in combination with RecursiveArrayIterator classes can be used to replace array values on a multidimensional array at any level deep. RecusiveIteratorIterator 与 RecursiveArrayIterator 类结合可用于替换任意深度的多维数组上的数组值。

$array_iterator = new RecursiveArrayIterator($tree);
$recursive_iterator = new RecursiveIteratorIterator($array_iterator, RecursiveIteratorIterator::CHILD_FIRST);

foreach ($recursive_iterator as $key => $value) {
    if (is_array($value) && array_key_exists('children', $value)) {
        $array_with_children = $value;
        $array_with_children_count = $array_with_children['count'];

        foreach ($array_with_children['children'] as $children) {
            $array_with_children_count = $array_with_children_count + $children['count'];
        }

        $array_with_children['count'] = $array_with_children_count;
        $current_depth = $recursive_iterator->getDepth();

        for ($sub_depth = $current_depth; $sub_depth >= 0; $sub_depth--) {
            // Get the current level iterator
            $sub_iterator = $recursive_iterator->getSubIterator($sub_depth);

            // If we are on the level we want to change, use the replacements
            // ($array_with_children) other wise set the key to the parent
            // iterators value
            if ($sub_depth === $current_depth) {
                $value = $array_with_children;
            } else {
                $value = $recursive_iterator->getSubIterator(($sub_depth + 1))->getArrayCopy();
            }

            $sub_iterator->offsetSet($sub_iterator->key(), $value);
        }
    }
}

print_r($recursive_iterator->getArrayCopy());

See in gist .要点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM