1

I am looking to extract the total sum of complete multidimensional array, including the embedded sub-arrays.

I understand you can extract the top level but it will not provide you the sub-arrays sum. Also each extraction of sub-array will only sum that specific sub-array.

The key names are intentionally left as numbers since my bigger script will constantly grow the array. Due to this growth of sub-array, the sub-arrays can be 1 but also 100.

Question:

How can I extract the value of the total multidimensional array including its sub-arrays.

[$array_1] works as expected:

<?php

/**
 * [array_1]
 * Array
 */

    $array_1 = [1, 2, 4];
?>
<pre>

<?php
    print_r($array_1);



    echo("Sum complete array_1: " . array_sum($array_1));

[$array_2] Cannot get the total value.

/**
 * [array_2]
 * Multidimensional array.
 */

    $array_2 = [

            [
                1, 2, 7
            ],

            [
                4, 5, 6
            ]

    ];
?>

<pre>
<?php

    print_r($array_2);

    // [$array_2].
    echo("Sum complete array_2: " . array_sum($array_2)); // Returns zero.
    echo("\n\n");
    echo("Sum sub-array [0]:" . array_sum($array_2["0"])); // Returns 10.
    echo("\n\n");
    echo("\n\n");

Expected outcome [$array_2]:

25

Toolbox
  • 2,333
  • 12
  • 26

5 Answers5

3

One-liner,

echo array_sum(array_merge(...$array_2));

I am exposing inner arrays by flattening them to 1d and then adding them using array_sum.

array_merge — Merge one or more arrays

... called a splat operator Which exposed array as separated arguments to count of array.

EDIT

Here is one more,

echo array_sum(array_reduce($array_2, 'array_merge', []));

Output: 25

Rahul
  • 18,271
  • 7
  • 41
  • 60
1

array_reduce is the best for situations like these. You collect sum of each individual sub array in a variable and return them.

<?php

 $array_2 = [

            [
                1, 2, 7
            ],

            [
                4, 5, 6
            ]

];



$sum = array_reduce($array_2,function($sum,$value){
        $sum += array_sum($value);
        return $sum;
});

echo $sum;

Also note that this would consume very less space compared to array merge since your each subarray could be of length 100 or more and there could be 1000 such subarrays at the very least(since you said your array could grow).

nice_dev
  • 17,053
  • 2
  • 21
  • 35
  • The OP's array is too small to make a difference. – nice_dev Jun 13 '19 at 08:09
  • @DrakulaPredator You could have tried it yourself. Anyway, see your [first](https://3v4l.org/abrWO) and [second](https://3v4l.org/ALRev) approach with [mine](https://3v4l.org/33SCL) – nice_dev Jun 13 '19 at 08:56
0

You can use array_map (iterate through each sub-array) with array_sum

$t = 0;
array_map(function($v) use (&$t){ $t += array_sum($v);}, $array_2);

Working example

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
0

You can do the Fastest way,

<?php

$tot = 0;
foreach($array_2 as $value){
    $tot +=array_sum($value);
}
echo $tot;

OUTPUT:

25

DEMO

Ghanshyam Nakiya
  • 1,602
  • 17
  • 24
0
array_sum(call_user_func_array('array_merge', $array_2))
GMarco24
  • 410
  • 3
  • 10