簡體   English   中英

PHP具有相同鍵的求和數組值

[英]PHP sum array values with same keys

這是原始的主數組:

數組(

[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
            )

    )   

我只需要使用相同的鍵添加數組[categories]的值,並進一步使用它來打印總數,但沒有獲得正確的輸出,有人可以幫助我獲得所需的結果:

所需結果

具有相同鍵但單個數組值總計的數組

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

注意:初始數組是動態的,可以有n個鍵值對

謝謝

您需要做的第一件事是遍歷外部數組。 然后,對於外部數組中的每一行,您將遍歷category元素中的每個條目。 因此,這意味着我們有兩個foreach循環。 在內部foreach內部,我們只需將當前索引的值設置為'sum'數組上相同索引的值(如果尚不存在),或者如果該索引已存在於該索引中,則遞增該值“和”數組。

<?php
$sumArray = array();

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

使用示例數組進行演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM