我有两个具有这种结构的数组
array1 = Array(0=>Array(4, 7, 0, 11), 1=> Array(5,7,6,18));
array2 = Array(0=>Array(5,1,7,13), 1=>Array(9,1,6,16));
我尝试像这样按元素计算该数组的总和
=> 4 + 5 = 9;
Array(0=>Array(9,8,7,24), 1=>Array(14, 8, 12, 34));
尝试一下:
$array1 = [[4, 7, 0, 11], [5, 7, 6, 18]];
$array2 = [[5, 1, 7, 13], [9, 1, 6, 16]];
$output = array_map(function($a, $b){
return array_map(function() {
return array_sum(func_get_args());
}, $a, $b);
}, $array1, $array2);
var_dump($output);
输出:
array (size=2)
0 =>
array (size=4)
0 => int 9
1 => int 8
2 => int 7
3 => int 24
1 =>
array (size=4)
0 => int 14
1 => int 8
2 => int 12
3 => int 34