470,849 Members | 1,014 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

sum two arrays by indexes

I'm looking for a short way of suming the indexes of two arrays and
returning one array as the result. I'm not referring to what
array_sum() is capable of doing. Below will get it done; but is there
any built in function doing the same??

function sum_by_index($array1,$array2) {
for ($i = 0; $i <= (count($array1) -1); $i++) {
$temp[$i] = $array1[$i] + $array2[$i];
}

return $temp;
}//note both $array1 and $array2 are to be of the same size
Regards,
Gerard.

Jan 3 '06 #1
3 6363
Gerard Matthew wrote (in part):
I'm looking for a short way of suming the indexes of two arrays and
returning one array as the result. I'm not referring to what
array_sum() is capable of doing. Below will get it done; but is there
any built in function doing the same??

function sum_by_index($array1,$array2) {
for ($i = 0; $i <= (count($array1) -1); $i++) {
$temp[$i] = $array1[$i] + $array2[$i];
}

return $temp;
}//note both $array1 and $array2 are to be of the same size


The following function does the same as yours with the added
functionality that it works with associative arrays also:

function sum_by_index($ary1,$ary2)
{
$tmp = array();
foreach($ary1 as $k=>$v) $tmp[$k] = $v + $ary2[$k];
return($tmp);
}

In your function, why do you use

for ($i = 0; $i <= (count($array1) -1); $i++)

when

for ($i = 0; $i < count($array1); $i++)

works just as well (and almost everyone else uses it).

Ken

Jan 3 '06 #2

Gerard Matthew wrote:
I'm looking for a short way of suming the indexes of two arrays and
returning one array as the result. I'm not referring to what
array_sum() is capable of doing. Below will get it done; but is there
any built in function doing the same??
Not that I know of.
function sum_by_index($array1,$array2) {
for ($i = 0; $i <= (count($array1) -1); $i++) {
$temp[$i] = $array1[$i] + $array2[$i];
}

return $temp;
}//note both $array1 and $array2 are to be of the same size


Nothing wrong with that. Although it might be slightly more efficient
as such:

function sum_by_index(&$array1, &$array2) {
for($i = (count($array1) - 1); $i >= 0; $i++)
$temp[$i] = $array1[$i] + $array[$i];
return $temp;
}

Jan 3 '06 #3
If you want to look clever by baffling others:

$sum = array_map('array_sum', array_map(null, $array1, $array2));

Jan 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Thomas Bunce | last post: by
27 posts views Thread by Abdullah Kauchali | last post: by
34 posts views Thread by Christopher Benson-Manica | last post: by
3 posts views Thread by Dr. Len | last post: by
5 posts views Thread by Michal Táborský | last post: by
11 posts views Thread by Paul Lautman | last post: by
11 posts views Thread by Bosconian | last post: by
3 posts views Thread by sjsean | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.