473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 6981
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Thomas Bunce | last post by:
I am new at Pyton and I am learning from book not classes so please forgive my being slow The below does not work I get an Error of File "Matrix = k NameError: name 'iMatrix' is not defined" ...
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
34
by: Christopher Benson-Manica | last post by:
If an array is sparse, say something like var foo=; foo=4; foo='baz'; foo='moo'; is there a way to iterate through the entire array? --
3
by: Dr. Len | last post by:
Hi NG, is it possible for struct to have a fixed-length array of primitive types as member variable as in C++, such that the array doesn't need to be allocated explicitly? Eg.: --------...
2
by: John | last post by:
Hello everyone, I'm currently writing a program to keep track of schedule changes at a school. The goal is to have someone using the program to declare changes, then the program writes a html...
5
by: Michal Táborský | last post by:
I am wondering, if it's effective to use text arrays to store multilanguage information. We used to do it like this: CREATE TABLE product ( id serial NOT NULL, price float4, ... )
11
by: Paul Lautman | last post by:
I'm having some trouble understanding what is happening with some array sorting functions. In all cases, my compare function is: function compare($x, $y) { if ( $x == $y ) return 0; else if (...
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
3
by: sjsean | last post by:
All thanks in advance for reading my post. I am new to using js and more accustomed to vbscript. I had written code which created a shopping cart into an array using vbscript and then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.