duanci9305 2018-02-01 12:16
浏览 55
已采纳

多维数组中的求和值?

I just have no idea how to sum and echo values in multidimensional array. This is my array with info about order.

I need to sum RESULT_VALUE and echo one united DESCR list. I have tried lots of things but nothing, never worked with that kind of arrays levels. This is an array with two different products with applied discounts.

Array
(
    [0] => Array
        (
            [ORDER_ID] => 35
            [DISCOUNT_ID] => 18
            [ORDER_COUPON_ID] => 0
            [COUPON_ID] => 
            [RESULT] => Array
                (
                    [BASKET] => Array
                        (
                            [617] => Array
                                (
                                    [RULE_ID] => 248
                                    [APPLY] => Y
                                    [RULE_DESCR_ID] => 248
                                    [ACTION_BLOCK_LIST] => Array
                                        (
                                            [0] => 0
                                        )

                                    [DESCR_DATA] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [TYPE] => 2
                                                    [VALUE] => 10
                                                    [VALUE_TYPE] => P
                                                    [VALUE_ACTION] => D
                                                    [RESULT_VALUE] => 229.905
                                                    [RESULT_UNIT] => RUB
                                                )

                                        )

                                    [DESCR] => Array
                                        (
                                            [0] => discount 10% (229.91 rub.)
                                        )

                                    [DESCR_ID] => 248
                                    [BASKET_ID] => 617
                                    [MODULE] => catalog
                                    [PRODUCT_ID] => 10109
                                )

                            [618] => Array
                                (
                                    [RULE_ID] => 249
                                    [APPLY] => Y
                                    [RULE_DESCR_ID] => 249
                                    [ACTION_BLOCK_LIST] => Array
                                        (
                                            [0] => 0
                                        )

                                    [DESCR_DATA] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [TYPE] => 2
                                                    [VALUE] => 10
                                                    [VALUE_TYPE] => P
                                                    [VALUE_ACTION] => D
                                                    [RESULT_VALUE] => 70.796
                                                    [RESULT_UNIT] => RUB
                                                )

                                        )

                                    [DESCR] => Array
                                        (
                                            [0] => discount 10% (70.80 rub.)
                                        )

                                    [DESCR_ID] => 249
                                    [BASKET_ID] => 618
                                    [MODULE] => catalog
                                    [PRODUCT_ID] => 10054
                                )

                        )

                )

            [ACTION_BLOCK_LIST] => 1
        )
  • 写回答

3条回答 默认 最新

  • dssu33392 2018-02-01 13:51
    关注

    You can use array_walk_recursive to get result. Code below sum all values of key $key wherever they are in the array

    $key = 'RESULT_VALUE';
    $sum = 0;
    array_walk_recursive ($array, 
             function($v, $k, $key) use (&$sum) { 
                 if($k === $key) { $sum += $v;}}, 
             $key);
    
    echo $sum;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?