Home > database >  PHP associate array values have to sum
PHP associate array values have to sum

Time:06-15

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 of the array values and get the result in below format,

Example for array result

[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 for :

$sum = 0; 
foreach($ar as $k=>$res2){ 
    $sum[$k][] = $res2 $sum; 
} 

CodePudding user response:

a) If keys are fixed, You need a simple foreach() loop along with array_keys(),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 below code:

$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

CodePudding user response:

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];
            }
        }
    }
  • Related