简体   繁体   English

PHP具有相同键的求和数组值

[英]PHP sum array values with same keys

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: 我只需要使用相同的键添加数组[categories]的值,并进一步使用它来打印总数,但没有获得正确的输出,有人可以帮助我获得所需的结果:

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 注意:初始数组是动态的,可以有n个键值对

Thanks 谢谢

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. 然后,对于外部数组中的每一行,您将遍历category元素中的每个条目。 So this means that we have two foreach loops. 因此,这意味着我们有两个foreach循环。 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. 在内部foreach内部,我们只需将当前索引的值设置为'sum'数组上相同索引的值(如果尚不存在),或者如果该索引已存在于该索引中,则递增该值“和”数组。

<?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 使用示例数组进行演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM