doumi7861 2016-06-23 18:07
浏览 193
已采纳

PHP使用相同的键求和数组值

This is the original main array:

Array (

[0] => Array

    (
        [subtotal] => 0.6000
        [taxes] => 0.0720
        [charged_amount] => 0.6720
        [total_discount] => 0.0000
        [provinceName] => BC
        [store_key] => 1
        [store_id] => 5834
        [categories] => Array
            (
                [2] => 0.6000
                [4] => 0
                [3] => 0
            )

    )

[1] => Array
    (
        [subtotal] => 29.8500
        [taxes] => 2.3270
        [charged_amount] => 20.2370
        [total_discount] => 11.9400
        [provinceName] => MB
        [store_key] => 9
        [store_id] => 1022
        [categories] => Array
            (
                [2] => 0
                [4] => 29.8500
                [3] => 0
            )

    )

[2] => Array
    (
        [subtotal] => 0.3000
        [taxes] => 0.0390
        [charged_amount] => 0.3390
        [total_discount] => 0.0000
        [provinceName] => NB
        [store_key] => 8
        [store_id] => 1013
        [categories] => Array
            (
                [2] => 0.3000
                [4] => 0
                [3] => 0
            )

    )

[3] => Array
    (
        [subtotal] => 24.3100
        [taxes] => 1.1830
        [charged_amount] => 10.2830
        [total_discount] => 15.2100
        [provinceName] => NL
        [store_key] => 4
        [store_id] => 3033
        [categories] => Array
            (
                [2] => 24.3100
                [4] => 0
                [3] => 0
            )

    )

[4] => Array
    (
        [subtotal] => 1116.3400
        [taxes] => 127.6960
        [charged_amount] => 1110.0060
        [total_discount] => 134.0300
        [provinceName] => ON
        [store_key] => 2
        [store_id] => 1139
        [categories] => Array
            (
                [2] => 85.7300
                [4] => 143.2800
                [3] => 887.3300
            )

    )

[5] => Array
    (
        [subtotal] => 10.8500
        [taxes] => 1.4100
        [charged_amount] => 12.2600
        [total_discount] => 0.0000
        [provinceName] => ON
        [store_key] => 5
        [store_id] => 1116
        [categories] => Array
            (
                [2] => 10.8500
                [4] => 0
                [3] => 0
            )

    )   

)

I just need to add the values of the array [categories] with same keys and use it further to print the total, but not getting correct output, can someone help me out to get the desired result:

Desired result

An array with same keys but total of individual array values

Array ( [2] => 0.9000 [4] => 29.8500 [3] => 1.5 ) 

NOTE: Initial array is dynamic can have n number of key value pair

Thanks

  • 写回答

1条回答 默认 最新

  • ds08541 2016-06-23 21:05
    关注

    The first thing that you need to do is iterate through the outer array. Then, for each row in the outer array, you and to iterate through each entry in the category element. So this means that we have two foreach loops. Inside the inner foreach, we simply set the value for the current index to be the value of the same index on a 'sum' array (if it doesn't already exist), or increment the value of that index if it already exists in the 'sum' array.

    <?php
    $sumArray = array();
    
    foreach($outerArray as $row)
    {
        foreach($row["categories"] as $index => $value)
        {
            $sumArray[$index] = (isset($sumArray[$index]) ? $sumArray[$index] + $value : $value);
        }
    }
    ?>
    

    Demo using your example array

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站