-6

How do I get the sum of this arrays.

$arrays = [[0, 176000], [0,500], [0, 3960000]];

The output should be:

$arrays = [0, 4136500];

Any help will be much appreciated.

5
  • 2
    your array is wrong otherwise your output is not right! Commented Nov 5, 2018 at 7:29
  • What do you mean wrong? Commented Nov 5, 2018 at 7:31
  • , is elements delimiter in array. Numbers don't use it. Commented Nov 5, 2018 at 7:32
  • Okay already updated my arrays. Commented Nov 5, 2018 at 7:33
  • What have you tried so far? Commented Nov 5, 2018 at 7:34

5 Answers 5

2

Using variadic arguments and null callback for first array_map:

$arrays = [[0, 176000], [0,500], [0, 3960000]];
print_r(array_map('array_sum', array_map(null, ...$arrays)));
Sign up to request clarification or add additional context in comments.

3 Comments

Im getting this error: "array_sum() expects parameter 1 to be array, integer given"
@u_mulder I used return instead of print_r.
As @Nick provides a working fiddle, then I suppose you showed us not correct data or applied my code wrong.
0

In Larevel you can use collections. For example:

    $a = collect([[0, 176000], [0,500], [0, 3960000]]);
    $b = new Collection;
    foreach ($a as $k => $e) {
        $b->push ( $a->pluck($k)->sum() );
    }

    $c = $b->take(2); // Result is going to be here

4 Comments

Thanks. But I'am getting 3 items [ 0, 4136500, 0 ]. It should be [ 0, 4136500 ]
However the $b->take(2) would be dynamic I can take 3
In your case try to use $c = $b->take($e->count()); in loop.
the $e is out of scope?
0

this is using native way (but i think need to rework)

<?php
$arrays = [[0, 176000], [0,500], [0, 3960000]];
$new_arrays = [];
foreach ($arrays as $key => $val) {
  $new_arrays[0] += $val[0];
  $new_arrays[1] += $val[1];
}
print_r($new_arrays);

3 Comments

Thanks. I think so because the array index should be dynamic.
can you more specific on what do want to achieve? @Jearson
$ilubis what if I have [[0,17600, 200]]
0

U_mulders answer works if there is no keys to to keep track of.

If you have an associative array then you need to loop the array and build add the values like:

$arrays = [[0,"A" => 176000], [0, "b" => 500], [0, 3960000, 200], [10, 500, 200]];

foreach($arrays as $sub){
    foreach($sub as $key => $num){
        if(!isset($new[$key])) $new[$key] =0;
        $new[$key] += $num;
    }
}
var_dump($new);

Output:

array(5) {
  [0]=>
  int(10)
  ["A"]=>
  int(176000)
  ["b"]=>
  int(500)
  [1]=>
  int(3960500)
  [2]=>
  int(400)
}

https://3v4l.org/NbNVg

2 Comments

Yeah mulder answer is correct for native php but for collections in laravel converting to Array is not working
@Jearson What are you converting? Your question says nothing about converting
0

There is no need to call array_map() more than once. Unpack the array using the spread operator (...). This will deliver columns of data to the custom function's body. Return the sum of each column.

Code: (Demo)

$arrays = [[0, 176000], [0,500], [0, 3960000]];
var_export(
    array_map(fn() => array_sum(func_get_args()), ...$arrays)
);

Or

var_export(
    array_map(fn(...$col) => array_sum($col), ...$arrays)
);

Output (from either)

array (
  0 => 0,
  1 => 4136500,
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.