1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| $array1 = array('1'=>'1','2'=>'2','3'=>'3');
$array2 = array( '2'=>'1','3'=>'2','4'=>'3');
$array3 = array( '3'=>'1','4'=>'2','5'=>'3');
$array = array_sum_values( $array1, $array2, $array3 );
print_r($array);
/**
* Sums the values of the arrays be there keys (PHP 4, PHP 5)
* array array_sum_values ( array array1 [, array array2 [, array ...]] )
*/
function array_sum_values() {
$return = array();
$intArgs = func_num_args();
$arrArgs = func_get_args();
if($intArgs < 1) trigger_error('Warning: Wrong parameter count for array_sum_values()', E_USER_WARNING);
foreach($arrArgs as $arrItem) {
if(!is_array($arrItem)) trigger_error('Warning: Wrong parameter values for array_sum_values()', E_USER_WARNING);
foreach($arrItem as $k => $v) {
$return[$k] += $v;
}
}
return $return;
}
/* result:
Array
(
[1] => 1
[2] => 3
[3] => 6
[4] => 5
[5] => 3
)
*/ |
Partager