Help merging arrays
Date: 07/21/05
(PHP Community) Keywords: php
http://us2.php.net/manual/en/ref.array.php
Data:
Inv_itmsA
(
[3] => 1782.00
[4] => 1022.00
[5] => -480.00
[6] => 1653.00
[7] => 1000.00
[8] => 1240.00
[10] => 6200.00
[11] => 6200.00
[12] => 0.00
[13] => 311.00
)
Inv_itmsB
(
[6] => 19.00
[7] => 40.00
[9] => 0.00
[13] => 99.99
)
Inv_itmsC
(
[3] => 588.00
[4] => 99.00
[5] => 46.80
[6] => 0.00
[7] => 0.00
[8] => 84.00
[9] => 0.00
[10] => 0.00
[11] => 0.00
[12] => 0.00
[13] => 99.65
)
Result I am seeking will create one array where keys that are the same result in a value that is the sum.
Inv_itms
(
[3] => 1782.00 + 588.00
[4] => 1022.00 + 99.00
[5] => -480.00 + 46.80
[6] => 1653.00 + 19.00 + 0.00
[7] => 1000.00 + 40.00 + 0.00
[8] => 1240.00 + 84.00
[9] => 0.00 + 0.00
[10] => 6200.00 + 0.00
[11] => 6200.00 + 0.00
[12] => 0.00 + .00
[13] => 311.00 + 99.99 +99.65
)
This is my working solution, but I was hoping for a more concise, elegant way.
$Inv_itmsM = $Inv_itmsA + $Inv_itmsB + $Inv_itmsC;
ksort($Inv_itmsM);
$Inv_itms_keys = array_keys($Inv_itmsM);
$i=0;
while($i < count($Inv_itms_keys)) {
$k = $Inv_itms_keys[$i];
$Inv_itms[$k]=$Inv_itmsA[$k] + $Inv_itmsB[$k] + $Inv_itmsC[$k];
$i++;
}
Source: http://www.livejournal.com/community/php/323323.html