3

Suppose that I have two arrays, and I want to add the corresponding elements.

$v_4 = [
    [1, 2, 3], 
    [4, 5, 6],
];
$v_5 = [
    [7, 8, 9], 
    [10, 11, 12], 
];

How should I construct an addition function to add across these arrays to get:

[
    [8, 10, 12], 
    [14, 16, 18],
]

I know I need to utilise array_map somehow, but I am unsure how to proceed in this multidimensional case.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
user3749854
  • 75
  • 1
  • 8

2 Answers2

1

You can use

$new = array();
foreach(array_map(null, $v_4, $v_5) as $var) {
    $data = call_user_func_array("array_map", array_merge(array(null) , $var));
    $new[] = array_map("array_sum", $data);
}
print_r($new);

See Live DEMO

The Example above is only limited to 2 arrays .. if you have more like 10 .. you can use this function with a little modification

print_r(array_sum_colums($v_4,$v_5,$v_6));

Or

print_r(array_sum_colums(array($v_4,$v_5,$v_6)));

See Live DEMO

Function

function array_sum_colums() {
    $args = count(func_get_args()) == 1 ? func_get_arg(0) : func_get_args();
    $arg = call_user_func_array("array_map", array_merge(array(null),$args));
    $new = array();
    foreach($arg as $var) {
        $data = call_user_func_array("array_map", array_merge(array(null), $var));
        $new[] = array_map("array_sum", $data);
    }
    return $new ;
}
Baba
  • 94,024
  • 28
  • 166
  • 217
  • Can you please explain `array_merge(array(null), $var)`? What is the importance of `array(null)` and what does `$var` contain at that point? – Micha Wiedenmann Jun 10 '13 at 05:24
  • array_merge only takes array ..... `array(null)` is a way of adding a null values to the array. You can use `var_dump($var)` to see what var contains – Baba Jun 10 '13 at 06:32
0

Synchronously isolate respective rows from both arrays, then sum the columns within each grouped payload of row data.

The outer call of array_map() isolates the rows from each array, the inner call of array_map() isolates the columns from the grouped rows.

Code (Demo)

var_export(
    array_map(
        fn(...$row) =>
            array_map(
                fn(...$col) => array_sum($col),
                ...$row
            ),
        $v_4,
        $v_5
    )
);

A less dynamic, more performant approach which is suitable for your exact sample data and much easier to read is to simply use nested loops and synchronously iterate each element in each of the two arrays. Of course this is only suitable because both arrays have exactly the same structure.

Code: (Demo)

$result = [];
foreach ($v_4 as $i => $row) {
    foreach ($row as $k => $v) {
        $result[$i][$k] = $v + $v_5[$i][$k];
    }
}
var_export($result);

In fact, if you don't mind modifying the first array as you iterate, you can simply add the corresponding values from the second array directly onto the first array elements. (Demo)

foreach ($v_4 as $i => &$row) {
    foreach ($row as $k => &$v) {
        $v += $v_5[$i][$k];
    }
}
var_export($v_4);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136