phpmatrixsumrowarray-map

Sum two array rows in PHP on a single line


I remember once I came across some website where sum of 2 arrays items was performed on a single line using array_sum and array_map functions. Does anyone know how to do that?

$a=array(1,2,3,4,5);
$b=array(0,1,0,1,0);
$result=compoundedSinlgeLineFunction($a,$b);
$result=array(1,3,3,5,5); //this is what we get

Solution

  • $result = array_map("array_sum", $a, $b);
    

    Result: (Demo)

    Fatal error: Uncaught ArgumentCountError: array_sum() expects exactly 1 argument, 2 given