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

时间:2018-09-18 14:16:06

标签: php arrays

在下面的数组中,最深的数组 Sub Belt 有4个项目。我要将这4个项目添加到其父类别皮带,因此皮带的计数值应为6。

现在帽子皮带有2、6个项目,父类别为附件。因此,附件的计数值应为8。

T恤连帽附件分别有5、3、8件。因此,父类别服装的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
        )

)

结果数组应如下所示:

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
        )

)

1 个答案:

答案 0 :(得分:0)

我从这里有了一个主意,http://php.net/manual/en/class.recursiveiteratoriterator.php#120131

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

请参见gist