0

I need a little help with summing up the columns in a two-dimensional array.

Eg Multidimensional array

Array
(
    [0] => Array
        (
            [0] => 30
            [1] => 5
            [2] => 6
            [3] => 7
            [4] => 8
            [5] => 9
            [6] => 2
            [7] => 5
        )

    [1] => Array
        (
            [0] => 50
            [1] => 4
            [2] => 8
            [3] => 4
            [4] => 4
            [5] => 6
            [6] => 9
            [7] => 2
        )
)

I want to have a result array that will hold the columnar sums of both rows.

Array
(
    [0] => 80
    [1] => 9
    [2] => 14
    [3] => 11
    [4] => 12
    [5] => 15
    [6] => 11
    [7] => 7
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136

3 Answers3

3

This should work for you:

<?php

    $arr = array(
            array(30, 5, 6, 7, 8, 9, 2, 5), 
            array(50, 4, 8, 4, 4, 6, 9, 2)
        );

    $result = array();

    foreach($arr[0] as $k => $v)
        $result[$k] = array_sum(array_column($arr, $k));

    print_r($result);

?>

Output:

Array ( [0] => 80 [1] => 9 [2] => 14 [3] => 11 [4] => 12 [5] => 15 [6] => 11 [7] => 7 )

EDIT:

If your php version is under 5.4 you can use this simple workaround:

function arrayColumn(array $array, $column_key, $index_key=null){
    if(function_exists('array_column ')){
        return array_column($array, $column_key, $index_key);
    }
    $result = [];
    foreach($array as $arr){
        if(!is_array($arr)) continue;

        if(is_null($column_key)){
            $value = $arr;
        }else{
            $value = $arr[$column_key];
        }

        if(!is_null($index_key)){
            $key = $arr[$index_key];
            $result[$key] = $value;
        }else{
            $result[] = $value;
        }

    }

    return $result;
}

(Note that you have to use arrayColumn())

From the manual comments: http://php.net/manual/de/function.array-column.php#116301

Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • Thanks Rizier but i am using php v 5.4 and it doesn't support array_column else it was a perfect answer Cheers!!! – Pradeep Srivastava Jan 24 '15 at 18:06
  • I liked your first algorithm for its simplicity, don't like your workaround though. I've been benchmarking your algorithm and mine. It took mine 0.16s to get the result in an array of 100000 subarrays of 8 elements and it took 0.97s yours... It gets worse when the subarrays are bigger: array of 100000 subarrays of 20 elements: 0.45s mine, 2.77s yours – acontell Jan 24 '15 at 21:41
2

This should work for arrays like the one of your example ($arr is an array like the one in your example, I haven't defined it here for simplicity's sake):

$res = array();
foreach($arr as $value) {
    foreach($value as $key => $number) {
        (!isset($res[$key])) ?
            $res[$key] = $number :
            $res[$key] += $number;
    }
}

print_r($res);
acontell
  • 6,792
  • 1
  • 19
  • 32
0

This answer is CERTAINLY not suitable with PHP5.4. The arrow function syntax became available as of PHP7.4.

Use the spread operator to unpack the input 2d array as separate row when fed to array_map(). This will send multiple values per column from each row. Using variadic data collection inside of the arrow function's argument signature ((...$col)) results in a single array variable containing all columnar values. Feed that array to array_sum() to get the desired result.

Code: (Demo)

$array = [
    [30, 5, 6, 7, 8, 9, 2, 5], 
    [50, 4, 8, 4, 4, 6, 9, 2]
];

var_export(
    array_map(
        fn(...$col) => array_sum($col),
        ...$array
    )
);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136