1

I have a associate array with some values in the below format, I need to sum the array based on the key values.

$ar = array(
    'Services' => array(
        array(
            '9' => '0',
            '10' => '0',
            '11' => '0',
            '12' => '0',
            '13' => '0',
            '14' => '0',
            '15' => '600'

        ),
    ),
    'Financial' => array(
        array(
            '9' => 0,
            '10' => 0,
            '11' => 0,
            '12' => 0,
            '13' => 0,
            '14' => 0,
            '15' => 500
        ),

        array(
            '9' => 0,
            '10' => 0,
            '11' => 0,
            '12' => 0,
            '13' => 0,
            '14' => 0,
            '15' => 200
        ),

        array(
            '9' => 0,
            '10' => 0,
            '11' => 0,
            '12' => 0,
            '13' => 0,
            '14' => 0,
            '15' => 300
        ),
    ),
    'DFD' => array(
        array(
            '9' => 0,
            '10' => 0,
            '11' => 0,
            '12' => 0,
            '13' => 0,
            '14' => 0,
            '15' => 1000,
        ),

        array(
            '9' => 0,
            '10' => 0,
            '11' => 0,
            '12' => 0,
            '13' => 0,
            '14' => 0,
            '15' => 100
        )
    )
);

I need to sum the array values and get the result in the following format:

[Services] => Array(
    [9] => 0,
    [10] => 0,
    [11] => 0,
    [12] => 0,, 
    [13] => 0, 
    [14] => 0,  
    [15] => 600 
    )
[Financial] => Array(
    [9] => 0,
    [10] => 0,
    [11] => 0,
    [12] => 0,, 
    [13] => 0, 
    [14] => 0,  
    [15] => 1000 
    )
[DFD] => Array(
    [9] => 0,
    [10] => 0,
    [11] => 0,
    [12] => 0,
    [13] => 0, 
    [14] => 0,  
    [15] => 1100 

)

Code I have tried so far:

$sum = 0; 
foreach($ar as $k=>$res2){ 
    $sum[$k][] = $res2+$sum; 
} 
Dharman
  • 30,962
  • 25
  • 85
  • 135
Poobalan
  • 25
  • 4

2 Answers2

3

a) If keys are fixed, You need a simple foreach() loop along with array_sum() and array_column()

$finalArray = [];

foreach($ar as $key=>$val){
    $finalArray[$key] = [
        array_sum(array_column($val,9)),
        array_sum(array_column($val,10)),
        array_sum(array_column($val,11)),
        array_sum(array_column($val,12)),
        array_sum(array_column($val,13)),
        array_sum(array_column($val,14)),
        array_sum(array_column($val,15))
    ];
}

print_r($finalArray);

Output : https://3v4l.org/V6Of4

b) In case child-array keys are dynamic, then use the below code (extra array_keys() method required as well):

$finalArray = [];

foreach($ar as $key=>$val){
    
    $keyIndexes = array_keys($val[0]);
    foreach($keyIndexes as $keyIndex){
        $finalArray[$key][$keyIndex] = array_sum(array_column($val,$keyIndex));
    }
}

Output : https://3v4l.org/mEC6l

c) In case you don't know which child array have more keys then you can try this code:

$finalArray = [];

foreach($ar as $key=>$val){
    $keyIndexes = array_keys($val[0]);
    if(count($val) > 1){
      $childWiseKeysCount = [];
      foreach($val as $v){
          $childWiseKeysCount[] = count($v);
      }
      $key = array_search(max($childWiseKeysCount),$childWiseKeysCount);
      $keyIndexes = array_keys($val[$key]);
    }
    
    foreach($keyIndexes as $keyIndex){
        $finalArray[$key][$keyIndex] = array_sum(array_column($val,$keyIndex));
    }
}

print_r($finalArray);

Output: http://tpcg.io/_A7CKPG

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
0

Here's one without using any special array_* functions to do the job. It will also assume dynamic arrays and that the first item represents the number of keys also present in the subsequent items that follow.

    $finalArray = Array();
    foreach($ar as $a=>$b){
        $finalArray[$a] = $b[0];
        foreach($b[0] as $k=>$v){
            for($i=1;$i<count($b);$i++){
                $finalArray[$a][$k] += $b[$i][$k];
            }
        }
    }
Watts Epherson
  • 692
  • 5
  • 9