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
)
2
  • 1
    what you tried till now? Commented Jan 24, 2015 at 17:49
  • Have you tried anyting? if so please post your code Commented Jan 24, 2015 at 17:52

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Rizier but i am using php v 5.4 and it doesn't support array_column else it was a perfect answer Cheers!!!
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
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);

2 Comments

Look at @Rizier123's answer. It's more efficient.
@zairwolf indeed it's a great answer, sadly not very compatible. I use PHP 5.4 and here it doesn't work (requires PHP >=5.5)
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
    )
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.