Page 1 of 1
Merge array - looking for easy solution
Posted: Thu Jan 19, 2006 2:34 am
by jmut
I have two arrays.
Code: Select all
[50037] => Array
(
[Tuesday] => 1
[Wednesday] => 7
[total] => 8
)
[50145] => Array
(
[Wednesday] => 1
[total] => 1
)
how could I sum them up.
So it becomes
Code: Select all
[merged] => Array
(
[Tuesday] => 1
[Wednesday] => 8
[total] => 9
)
Any shortcuts to acomplishing this.
I could itterate but was thinking maybe there is faster way.
Posted: Thu Jan 19, 2006 4:02 am
by duk
you can use array_combine(); to merge the two arrays
then using a foreach to pick up de keys and values and you just need to sum key with value
and set again the merged array with the sum values
Posted: Thu Jan 19, 2006 4:02 am
by Christopher
If they are simply two arrays then:
http://www.php.net/manual/en/function.array-merge.php. If they are arrays of arrays then you might need to use array_filer().
Posted: Thu Jan 19, 2006 4:26 am
by jmut
I don't think array merge will do the trick as I need sum up.
Yes they are only two arrays.
Anyhow I decided to foreach them.
Code: Select all
protected function sumArraysValues($arr1,$arr2) {
$final = array();
foreach ($arr1 as $key1 => $val1) {
if (!isset($arr2[$key1])) {
$final[$key1] = $val1;
} else {
$final[$key1] = $val1 + $arr2[$key1];
}
}
foreach ($arr2 as $key2 => $val2) {
if (!isset($final[$key2])) {
$final[$key2] = $val2;
}
}
return $final;
}
Posted: Thu Jan 19, 2006 4:35 am
by duk
should be something like this...
Code: Select all
$merged_array = array_combine($array1, $array2);
$i = 0;
foreach ($merged_array as $key => $value) {
$merged_array[$i] = $key + $value;
$i++;
}
Re: Merge array - looking for easy solution
Posted: Sat Jan 26, 2008 3:44 am
by eyespark
Hi,
I'm sorry to bring this old post back up. I have the same problem as jmut. His sumArraysValues function works as needed, I'm just wondering if there is any other solution. Just want to learn ...
Thanks in advance
Re: Merge array - looking for easy solution
Posted: Sat Jan 26, 2008 4:22 am
by JAM
Untested and quick, might be even easier solution...
Code: Select all
$array['50037'] = array('Tuesday' => 1, 'Wednesday' => 7, 'total' => 8);
$array['50145'] = array('Wednesday' => 1, 'total' => 1);
$array = array_merge_recursive($array['50037'], $array['50145']);
foreach($array as $k => $v) {
$array[$k] = (is_array($v) ? array_sum($v) : $v);
}
print_r($array);
Re: Merge array - looking for easy solution
Posted: Sat Jan 26, 2008 5:22 am
by eyespark
Thank you, but it is not working as exepected.
One more example:
I have two arrays with $item_id=>$item_quantity pairs.
Code: Select all
$array1 = array("1" => "30", "2" => "40", "3" => "50");
$array2 = array("1" => "10", "2" => "20", "4" => "70");
A resutling array should be
Code: Select all
$result = array("1" => "40", "2" => "60", "3" => "50", "4" => "70");
Re: Merge array - looking for easy solution
Posted: Sat Jan 26, 2008 5:42 am
by JAM
First of; you cant sum strings. So your line(as):
Code: Select all
$array1 = array("1" => "30", "2" => "40", "3" => "50");
...should be changed to:
Code: Select all
$array1 = array("1" => 30, "2" => 40, "3" => 50);
After that, this should work;
Code: Select all
function array_merge_keys(){
$args = func_get_args();
$result = array();
foreach($args as &$array){
foreach($array as $key => &$value){
$result[$key] += $value;
}
}
return $result;
}
$array1 = array("1" => 30, "2" => 40, "3" => 50);
$array2 = array("1" => 10, "2" => 20, "4" => 70);
$array = array_merge_keys($array1, $array2);
print_r($array);
Result:
Code: Select all
Array
(
[1] => 40
[2] => 60
[3] => 50
[4] => 70
)
Re: Merge array - looking for easy solution
Posted: Sat Jan 26, 2008 6:12 am
by eyespark
Beautiful! Thanks!