dsgdf45654 2015-06-24 15:31
浏览 14
已采纳

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

I cant get my head around how to do this.

Consider I have the following php array

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

I want to somehow iterate over each array element and total up the Product Count element based on its on value and all child values (which could be n levels deep)

The end result I am looking for is

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

can anyone help please? Speed or efficiency is not really a major issue as this will be run once a day at 3 in the morning so no-one will be waiting for it to finish its work!!

  • 写回答

1条回答 默认 最新

  • dsxml2169 2015-06-24 15:36
    关注

    The only reasonable way to tally up values in an array with unknown depth is by using recursive methods.

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧