Heim >Backend-Entwicklung >PHP-Tutorial >[伸手党]求个function,传入一个规律的二维数组,每个子数组都是键名相同的数组,键值都是整数,返回结果是二维数组多了一个子数组,是每个子数组的求和结果
如题
举例:
传入 array(0=>array(a=>2,b=>6),1=>array(a=>5,b=>7));
得到结果是 array(0=>array(a=>2,b=>6),1=>array(a=>5,b=>7),2=>array(a=>7,b=>11));
工期紧 能省一点时间是一点, 伸手求一次...
$ar = array( array( 'a' => 2, 'b' => 6), array( 'a' => 5, 'b' => 7),);$t = array();foreach($ar as $r) { foreach($r as $k=>$v) @$t[$k] += $v;}$ar[] = $t;print_r($ar);
Array( [0] => Array ( [a] => 2 [b] => 6 ) [1] => Array ( [a] => 5 [b] => 7 ) [2] => Array ( [a] => 7 [b] => 13 ))
感谢版大~~~
或
$ar = array( array( 'a' => 2, 'b' => 6), array( 'a' => 5, 'b' => 7),);$t = call_user_func_array('array_map', array_merge(array(null),$ar));$t = array_map('array_sum', $t);$t = array_combine(array_keys($ar[0]), $t);$ar[] = $t;print_r($ar);