使用其值和嵌套数组元素值总计数组元素

时间:2015-06-24 15:31:10

标签: php arrays multidimensional-array

我无法理解如何做到这一点。

考虑我有以下php数组

Array
(
    [0] => Array
        (
            [cat_id] => 2
            [parent_id] => 1
            [Title] => Default Category
            [Product Count] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [cat_id] => 4
                            [parent_id] => 2
                            [Title] => computers & electronics
                            [Product Count] => 5
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [cat_id] => 5
                                            [parent_id] => 4
                                            [Title] => projectors
                                            [Product Count] => 2
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [cat_id] => 6
                                                            [parent_id] => 5
                                                            [Title] => film projectors
                                                            [Product Count] => 1
                                                        )

                                    [1] => Array
                                        (
                                            [cat_id] => 7
                                            [parent_id] => 4
                                            [Title] => projector cases
                                            [Product Count] => 0
                                            children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [cat_id] => 8
                                                            [parent_id] => 7
                                                            [Title] => leather cases
                                                            [Product Count] => 10

我想以某种方式迭代每个数组元素,并根据其on值和所有子值(可能是n级深度)总计Product Count元素

我正在寻找的最终结果是

Array
(
    [0] => Array
        (
            [cat_id] => 2
            [parent_id] => 1
            [Title] => Default Category
            [Product Count] => 18
            [children] => Array
                (
                    [0] => Array
                        (
                            [cat_id] => 4
                            [parent_id] => 2
                            [Title] => computers & electronics
                            [Product Count] => 18
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [cat_id] => 5
                                            [parent_id] => 4
                                            [Title] => projectors
                                            [Product Count] => 3
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [cat_id] => 6
                                                            [parent_id] => 5
                                                            [Title] => film projectors
                                                            [Product Count] => 1
                                                        )

                                    [1] => Array
                                        (
                                            [cat_id] => 7
                                            [parent_id] => 4
                                            [Title] => projector cases
                                            [Product Count] => 10
                                            children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [cat_id] => 8
                                                            [parent_id] => 7
                                                            [Title] => leather cases
                                                            [Product Count] => 10

任何人都可以帮忙吗?速度或效率并不是一个主要问题,因为这将在每天早上3点运行一次,所以没有人会等待它完成它的工作!!

1 个答案:

答案 0 :(得分:1)

计算深度未知的数组中值的唯一合理方法是使用递归方法。

function sumItems(&$branch,$sum){

    $sum = $branch['Product Count'];
    foreach($branch['children'] as &$item){
          $sum += sumItems($item,$sum); // Each sub array runs this function
                                        // And adds thier count to the parent branch
    }

    $branch['Product Count'] = $sum; // Since branch is passed to function as
                                    // Reference, this line overrides the original value

    return $sum; // Each recursion of this function returns its own value + the sum of all sub arrays to the parent
}
sum_items($array,0); // Will return the sum of all branches and sub branches