0

Let's say I have this array in PHP

Array(
    [0] => Array
        (
            [DLVRD] => 2
            [FAILED] => 1
            [REJECT] => 4
            [QUEUED] => 5
        )

    [1] => Array
        (
            [DLVRD] => 5
            [FAILED] => 0
            [REJECT] => 3
            [QUEUED] => 2
        )

    [2] => Array
        (
            [DLVRD] => 3
            [FAILED] => 0
            [REJECT] => 1
            [QUEUED] => 3
        )
)

And I want to do is to have result something like this

Array
(
    [DLVRD] => 10
    [FAILED] => 1
    [OTHERS] => 8
)

Currently my PHP code is like this:

foreach($GetTelcosRevenue as $telco) {        
            if($telco['dr_detail'] == 'DLVRD') {
                $TelcoRevenue .="DLVRD:".$telco['TheTraffics'].",";
            }
            else if($telco['dr_detail'] == 'FAILED') {
                $TelcoRevenue .="FAILED:".$telco['TheTraffics'].",";
            }
            else {
                ?????
            }
        }
    }

I have tried to put $Others += $telco['TheTraffics'] and other hacks but I still got wrong result. Thanks for any help

Willy Mularto
  • 1,363
  • 4
  • 11
  • 11
  • 3
    Possible duplicate of [How to sum values of the array of the same key?](https://stackoverflow.com/questions/1496682/how-to-sum-values-of-the-array-of-the-same-key) – Arun Kumaresh Nov 22 '17 at 04:26

1 Answers1

0

You can do, by this way :

$sum = array();

foreach ($array as $k=>$sub) {  // $array is your question array
  foreach ($sub as $id=>$value) {
    $sum[$id]+=$value;
  }
}

print_r($sum);
helpdoc
  • 1,910
  • 14
  • 36