PHP expert |
|
Ik heb even een methode er voor geschreven; vrij experimenteel en ook nog behoorlijk generaliseerbaar, maar je kunt er je doel wel mee halen;
<?php
$a = array();
$a[11] = 9;
$a[12] = 8;
$a[10] = 7;
$a[4] = 6;
$b = array();
$b[6] = 8;
$b[10] = 7;
$b[11] = 6;
function MergeAndSum($a,$b) {
$result = array();
foreach($a as $key => $item) {
$result[$key] = $item;
if(isset($b[$key])) {
$result[$key] += $b[$key];
unset($a[$key]);
unset($b[$key]);
}
}
foreach($b as $key => $item) {
$result[$key] = $item;
if(isset($a[$key])) {
$result[$key] += $a[$key];
}
}
return $result;
}
echo '<pre>';
print_r(MergeAndSum($a,$b));
?>
<?php $a[11] = 9; $a[12] = 8; $a[10] = 7; $a[4] = 6; $b[6] = 8; $b[10] = 7; $b[11] = 6; function MergeAndSum($a,$b) { foreach($a as $key => $item) { $result[$key] = $item; $result[$key] += $b[$key]; } } foreach($b as $key => $item) { $result[$key] = $item; $result[$key] += $a[$key]; } } return $result; } ?>
|