PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
jmut
Forum Regular
Posts: 945 Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:
Post
by jmut » Thu Jan 19, 2006 2:34 am
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.
duk
Forum Contributor
Posts: 199 Joined: Wed May 19, 2004 8:45 am
Location: London
Post
by duk » Thu Jan 19, 2006 4:02 am
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
Last edited by
duk on Thu Jan 19, 2006 4:07 am, edited 2 times in total.
jmut
Forum Regular
Posts: 945 Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:
Post
by jmut » Thu Jan 19, 2006 4:26 am
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;
}
duk
Forum Contributor
Posts: 199 Joined: Wed May 19, 2004 8:45 am
Location: London
Post
by duk » Thu Jan 19, 2006 4:35 am
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++;
}
eyespark
Forum Commoner
Posts: 50 Joined: Tue Jan 24, 2006 7:36 am
Post
by eyespark » Sat Jan 26, 2008 3:44 am
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
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Sat Jan 26, 2008 4:22 am
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);
eyespark
Forum Commoner
Posts: 50 Joined: Tue Jan 24, 2006 7:36 am
Post
by eyespark » Sat Jan 26, 2008 5:22 am
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");
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Sat Jan 26, 2008 5:42 am
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
)
Last edited by
JAM on Sat Jan 26, 2008 6:32 am, edited 1 time in total.
eyespark
Forum Commoner
Posts: 50 Joined: Tue Jan 24, 2006 7:36 am
Post
by eyespark » Sat Jan 26, 2008 6:12 am
Beautiful! Thanks!