473,626 Members | 3,316 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding arrays

Consider the following multi-dimensional array:
---------------------------
$arr = array(
array(3, 5, 7, 9),
array(2, 4, 6, 8),
array(1, 3, 5, 7)
);

function add_arrays($arr ) {
for ($row = 0; $row < count($arr[0]); $row++) {
for ($column = 0; $column < count($arr[$column]); $column++) {
$totals[$row] = $totals[$row] + $arr[$column][$row];
}
}
print_r($totals );
}

add_arrays($arr );
---------------------------
This returns the following array:
Array ( [0] =6 [1] =12 [2] =18 [3] =24 )

These are the totals of the array's in $arr: 6 (3 + 2 + 1, added all
first elements), 12 (5 + 4 + 3, added all second elements), etc. This
took me quite a portion of the day to construct ;) However, I would
like add_arrays to return a multidimensiona l array like this:

Array (
Array ( [0] =3 [1] = 5 [2] = 7 [3] = 9 ) // 1st row
Array ( [0] =5 [1] = 9 [2] =13 [3] =17 ) // 1st row + second
row (i.e. 5 = 3 + 2)
Array ( [0] =6 [1] =12 [2] =18 [3] =24 ) // 1st _ 2nd + 3rd
row (i.e. 6 = 3 + 2 + 1)
)

Could somebody please help me building a function that does just that?
Adding one row to the previous one and adding the result to the output
array? Any help would be greatly apprectiated!

May 7 '07 #1
11 3140
Daz
On May 7, 4:05 pm, dennis.spreng.. .@gmail.com wrote:
Consider the following multi-dimensional array:
---------------------------
$arr = array(
array(3, 5, 7, 9),
array(2, 4, 6, 8),
array(1, 3, 5, 7)
);

function add_arrays($arr ) {
for ($row = 0; $row < count($arr[0]); $row++) {
for ($column = 0; $column < count($arr[$column]); $column++) {
$totals[$row] = $totals[$row] + $arr[$column][$row];
}
}
print_r($totals );

}

add_arrays($arr );
---------------------------
This returns the following array:
Array ( [0] =6 [1] =12 [2] =18 [3] =24 )

These are the totals of the array's in $arr: 6 (3 + 2 + 1, added all
first elements), 12 (5 + 4 + 3, added all second elements), etc. This
took me quite a portion of the day to construct ;) However, I would
like add_arrays to return a multidimensiona l array like this:

Array (
Array ( [0] =3 [1] = 5 [2] = 7 [3] = 9 ) // 1st row
Array ( [0] =5 [1] = 9 [2] =13 [3] =17 ) // 1st row + second
row (i.e. 5 = 3 + 2)
Array ( [0] =6 [1] =12 [2] =18 [3] =24 ) // 1st _ 2nd + 3rd
row (i.e. 6 = 3 + 2 + 1)
)

Could somebody please help me building a function that does just that?
Adding one row to the previous one and adding the result to the output
array? Any help would be greatly apprectiated!
Is there any guarantee that the arrays will all be the same length as
one-another? Are they always a set length, or can the lengths vary?

Personally, I would start with making an empty array, which is the
same length as the size of the longest array. Set all values in that
array to 0. Then, loop through each array, and loop through the values
of each array, and add them to the starting array. Now return the
array you just created. Hope that makes sense. Example to follow.

May 7 '07 #2
Daz
On May 7, 4:05 pm, dennis.spreng.. .@gmail.com wrote:
Consider the following multi-dimensional array:
---------------------------
$arr = array(
array(3, 5, 7, 9),
array(2, 4, 6, 8),
array(1, 3, 5, 7)
);

function add_arrays($arr ) {
for ($row = 0; $row < count($arr[0]); $row++) {
for ($column = 0; $column < count($arr[$column]); $column++) {
$totals[$row] = $totals[$row] + $arr[$column][$row];
}
}
print_r($totals );

}

add_arrays($arr );
---------------------------
This returns the following array:
Array ( [0] =6 [1] =12 [2] =18 [3] =24 )

These are the totals of the array's in $arr: 6 (3 + 2 + 1, added all
first elements), 12 (5 + 4 + 3, added all second elements), etc. This
took me quite a portion of the day to construct ;) However, I would
like add_arrays to return a multidimensiona l array like this:

Array (
Array ( [0] =3 [1] = 5 [2] = 7 [3] = 9 ) // 1st row
Array ( [0] =5 [1] = 9 [2] =13 [3] =17 ) // 1st row + second
row (i.e. 5 = 3 + 2)
Array ( [0] =6 [1] =12 [2] =18 [3] =24 ) // 1st _ 2nd + 3rd
row (i.e. 6 = 3 + 2 + 1)
)

Could somebody please help me building a function that does just that?
Adding one row to the previous one and adding the result to the output
array? Any help would be greatly apprectiated!
I take that back, I misread your post. Sorry.

Example to follow anyway. :D

May 7 '07 #3
On May 7, 11:05 am, dennis.spreng.. .@gmail.com wrote:
Consider the following multi-dimensional array:
---------------------------
$arr = array(
array(3, 5, 7, 9),
array(2, 4, 6, 8),
array(1, 3, 5, 7)
);

function add_arrays($arr ) {
for ($row = 0; $row < count($arr[0]); $row++) {
for ($column = 0; $column < count($arr[$column]); $column++) {
$totals[$row] = $totals[$row] + $arr[$column][$row];
}
}
print_r($totals );

}

add_arrays($arr );
---------------------------
This returns the following array:
Array ( [0] =6 [1] =12 [2] =18 [3] =24 )

These are the totals of the array's in $arr: 6 (3 + 2 + 1, added all
first elements), 12 (5 + 4 + 3, added all second elements), etc. This
took me quite a portion of the day to construct ;) However, I would
like add_arrays to return a multidimensiona l array like this:

Array (
Array ( [0] =3 [1] = 5 [2] = 7 [3] = 9 ) // 1st row
Array ( [0] =5 [1] = 9 [2] =13 [3] =17 ) // 1st row + second
row (i.e. 5 = 3 + 2)
Array ( [0] =6 [1] =12 [2] =18 [3] =24 ) // 1st _ 2nd + 3rd
row (i.e. 6 = 3 + 2 + 1)
)

Could somebody please help me building a function that does just that?
Adding one row to the previous one and adding the result to the output
array? Any help would be greatly apprectiated!
Here's one way. Obviously there are others, also.

function add_arrays_cumu lative($arr) {
$out = array();

for($i = 0; $i < count($arr); $i++) {
if(!isset($out[$i]))
$out[$i] = array();

for($j = 0; $j < count($arr[$i]); $j++)
$out[$i][$j] = $arr[$i][$j] + (isset($out[$i-1][$j]) ?
$out[$i-1][$j] : 0);
}

return $out;
}
May 7 '07 #4
The length of the array's may vary, as may the number of array's. But
all array's will be of equal length, i.e.

$arr = array(
array(3, 5, 7, 9, 23, 53, 54, 57),
array(2, 4, 6, 8, 23 , 2, 43, 5),
array(1, 3, 5, 7, 56, 45, 8, 54),
array(4, 4, 5, 3, 21, 3, 23, 42),
array(6, 3, 2, 1, 8, 33, 12, 43)
);

is also possible but

$arr = array(
array(3, 5, 7, 9, 23, 53, 54, 57),
array(2, 4, 6, 8, 23 , 2),
);

will never be :-) Hope this helps!
May 7 '07 #5
Wow, thanks! It works great. I will spend the next hour or so trying
to understand what code you wrote and why I still can't come up with
stuff solutions that :-)

On 7 mei, 17:32, ZeldorBlat <zeldorb...@gma il.comwrote:
On May 7, 11:05 am, dennis.spreng.. .@gmail.com wrote:
Consider the following multi-dimensional array:
---------------------------
$arr = array(
array(3, 5, 7, 9),
array(2, 4, 6, 8),
array(1, 3, 5, 7)
);
function add_arrays($arr ) {
for ($row = 0; $row < count($arr[0]); $row++) {
for ($column = 0; $column < count($arr[$column]); $column++) {
$totals[$row] = $totals[$row] + $arr[$column][$row];
}
}
print_r($totals );
}
add_arrays($arr );
---------------------------
This returns the following array:
Array ( [0] =6 [1] =12 [2] =18 [3] =24 )
These are the totals of the array's in $arr: 6 (3 + 2 + 1, added all
first elements), 12 (5 + 4 + 3, added all second elements), etc. This
took me quite a portion of the day to construct ;) However, I would
like add_arrays to return a multidimensiona l array like this:
Array (
Array ( [0] =3 [1] = 5 [2] = 7 [3] = 9 ) // 1st row
Array ( [0] =5 [1] = 9 [2] =13 [3] =17 ) // 1st row + second
row (i.e. 5 = 3 + 2)
Array ( [0] =6 [1] =12 [2] =18 [3] =24 ) // 1st _ 2nd + 3rd
row (i.e. 6 = 3 + 2 + 1)
)
Could somebody please help me building a function that does just that?
Adding one row to the previous one and adding the result to the output
array? Any help would be greatly apprectiated!

Here's one way. Obviously there are others, also.

function add_arrays_cumu lative($arr) {
$out = array();

for($i = 0; $i < count($arr); $i++) {
if(!isset($out[$i]))
$out[$i] = array();

for($j = 0; $j < count($arr[$i]); $j++)
$out[$i][$j] = $arr[$i][$j] + (isset($out[$i-1][$j]) ?
$out[$i-1][$j] : 0);
}

return $out;

}

May 7 '07 #6
Daz
On May 7, 4:40 pm, dennis.spreng.. .@gmail.com wrote:
Wow, thanks! It works great. I will spend the next hour or so trying
to understand what code you wrote and why I still can't come up with
stuff solutions that :-)

On 7 mei, 17:32, ZeldorBlat <zeldorb...@gma il.comwrote:
On May 7, 11:05 am, dennis.spreng.. .@gmail.com wrote:
Consider the following multi-dimensional array:
---------------------------
$arr = array(
array(3, 5, 7, 9),
array(2, 4, 6, 8),
array(1, 3, 5, 7)
);
function add_arrays($arr ) {
for ($row = 0; $row < count($arr[0]); $row++) {
for ($column = 0; $column < count($arr[$column]); $column++) {
$totals[$row] = $totals[$row] + $arr[$column][$row];
}
}
print_r($totals );
}
add_arrays($arr );
---------------------------
This returns the following array:
Array ( [0] =6 [1] =12 [2] =18 [3] =24 )
These are the totals of the array's in $arr: 6 (3 + 2 + 1, added all
first elements), 12 (5 + 4 + 3, added all second elements), etc. This
took me quite a portion of the day to construct ;) However, I would
like add_arrays to return a multidimensiona l array like this:
Array (
Array ( [0] =3 [1] = 5 [2] = 7 [3] = 9 ) // 1st row
Array ( [0] =5 [1] = 9 [2] =13 [3] =17 ) // 1st row + second
row (i.e. 5 = 3 + 2)
Array ( [0] =6 [1] =12 [2] =18 [3] =24 ) // 1st _ 2nd + 3rd
row (i.e. 6 = 3 + 2 + 1)
)
Could somebody please help me building a function that does just that?
Adding one row to the previous one and adding the result to the output
array? Any help would be greatly apprectiated!
Here's one way. Obviously there are others, also.
function add_arrays_cumu lative($arr) {
$out = array();
for($i = 0; $i < count($arr); $i++) {
if(!isset($out[$i]))
$out[$i] = array();
for($j = 0; $j < count($arr[$i]); $j++)
$out[$i][$j] = $arr[$i][$j] + (isset($out[$i-1][$j]) ?
$out[$i-1][$j] : 0);
}
return $out;
}
Here's my version with comments. I posted it anyway, as it would have
been a waste of time for me not to. I think it's identical to the
example above, but it's a little longer...

<?php

$arr = array(
array(3, 5, 7, 9),
array(2, 4, 6, 8),
array(1, 3, 5, 7)
);

function add_arrays($inp ut_array) {

// Cteate a new tmp_array in the global scope
// of the function.
$tmp_array;

// Create out array which will be returned
$out_array = array();

// Add the first row of $arr
$out_array[] = $input_array[0];

// For each array from the second array in $arr...
//echo "<pre>",print_r ($input_array[0]),"</pre>";
for ($row = 1; $row < sizeof($input_a rray); $row++) {

// Create a new array
$tmp_array = array();

// Now, for each key in the current $arr row...
for ($key_num = 0; $key_num < sizeof($input_a rray[$row]); $key_num+
+) {

// Add the values of $input_array[$row-1][$key_num] to the values
// of the array in $output_array before ($out_array[$row]
[$key_num])
$tmp_array[$key_num] = $input_array[$row][$key_num] +
$out_array[$row-1][$key_num];
}
// Append the $tmp_array to the $output_array
$out_array[] = $tmp_array;
}
return $out_array;

}

$result= add_arrays($arr );
echo 'Contents of $result: <br />';
echo "<pre>Array 1:<br />",print_r($res ult[0]),"</pre>";
echo "<pre>Array 2:<br />",print_r($res ult[1]),"</pre>";
echo "<pre>Array 3:<br />",print_r($res ult[2]),"</pre>";
?>

May 7 '07 #7
Daz
On May 7, 4:40 pm, dennis.spreng.. .@gmail.com wrote:
Wow, thanks! It works great. I will spend the next hour or so trying
to understand what code you wrote and why I still can't come up with
stuff solutions that :-)

On 7 mei, 17:32, ZeldorBlat <zeldorb...@gma il.comwrote:
On May 7, 11:05 am, dennis.spreng.. .@gmail.com wrote:
Consider the following multi-dimensional array:
---------------------------
$arr = array(
array(3, 5, 7, 9),
array(2, 4, 6, 8),
array(1, 3, 5, 7)
);
function add_arrays($arr ) {
for ($row = 0; $row < count($arr[0]); $row++) {
for ($column = 0; $column < count($arr[$column]); $column++) {
$totals[$row] = $totals[$row] + $arr[$column][$row];
}
}
print_r($totals );
}
add_arrays($arr );
---------------------------
This returns the following array:
Array ( [0] =6 [1] =12 [2] =18 [3] =24 )
These are the totals of the array's in $arr: 6 (3 + 2 + 1, added all
first elements), 12 (5 + 4 + 3, added all second elements), etc. This
took me quite a portion of the day to construct ;) However, I would
like add_arrays to return a multidimensiona l array like this:
Array (
Array ( [0] =3 [1] = 5 [2] = 7 [3] = 9 ) // 1st row
Array ( [0] =5 [1] = 9 [2] =13 [3] =17 ) // 1st row + second
row (i.e. 5 = 3 + 2)
Array ( [0] =6 [1] =12 [2] =18 [3] =24 ) // 1st _ 2nd + 3rd
row (i.e. 6 = 3 + 2 + 1)
)
Could somebody please help me building a function that does just that?
Adding one row to the previous one and adding the result to the output
array? Any help would be greatly apprectiated!
Here's one way. Obviously there are others, also.
function add_arrays_cumu lative($arr) {
$out = array();
for($i = 0; $i < count($arr); $i++) {
if(!isset($out[$i]))
$out[$i] = array();
for($j = 0; $j < count($arr[$i]); $j++)
$out[$i][$j] = $arr[$i][$j] + (isset($out[$i-1][$j]) ?
$out[$i-1][$j] : 0);
}
return $out;
}
Oh, and for the record, ZeldorBlat's example is much safer. My example
was primarily to help you understand one of the possible methods to
achieve what you wanted. I didn't want you to just copy it and use it
as it was. Hopefully my version will have helped you understand the
steps taken to achieve the results you wanted. If you require a better
explanation of anything (in ZeldorBlat's example, or my own), please
don't hesitate to ask.

May 7 '07 #8
@Daz - thanks! I will really help me understand. I will get back to
you if I'd have any questions :-)

May 7 '07 #9
de************* *@gmail.com wrote:
Consider the following multi-dimensional array:
---------------------------
$arr = array(
array(3, 5, 7, 9),
array(2, 4, 6, 8),
array(1, 3, 5, 7)
);

function add_arrays($arr ) {
for ($row = 0; $row < count($arr[0]); $row++) {
for ($column = 0; $column < count($arr[$column]); $column++) {
$totals[$row] = $totals[$row] + $arr[$column][$row];
}
}
print_r($totals );
}

add_arrays($arr );
---------------------------
This returns the following array:
Array ( [0] =6 [1] =12 [2] =18 [3] =24 )

These are the totals of the array's in $arr: 6 (3 + 2 + 1, added all
first elements), 12 (5 + 4 + 3, added all second elements), etc. This
took me quite a portion of the day to construct ;) However, I would
like add_arrays to return a multidimensiona l array like this:

Array (
Array ( [0] =3 [1] = 5 [2] = 7 [3] = 9 ) // 1st row
Array ( [0] =5 [1] = 9 [2] =13 [3] =17 ) // 1st row + second
row (i.e. 5 = 3 + 2)
Array ( [0] =6 [1] =12 [2] =18 [3] =24 ) // 1st _ 2nd + 3rd
row (i.e. 6 = 3 + 2 + 1)
)

Could somebody please help me building a function that does just that?
Adding one row to the previous one and adding the result to the output
array? Any help would be greatly apprectiated!
hi, how about

function plus($a, $b) { return $a + $b; }

$res = array(array_shi ft($arr));
foreach($arr as $row)
$res[] = array_map('plus ', end($res), $row);
simple, huh? ;))

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
May 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3604
by: David | last post by:
I have an array that contains numbers in string elements , i want to convert this to integers, i have made a for loop that uses the number of elements in the array and then adds 0, thereby converting it to an integer. //$chars is the array of strings for ($i=0; $i<$numpoints; $i++) { $array = array($chars + 0); } I dont know though how to add all these elements to the 1 array, it
0
4595
by: Björn | last post by:
Hello, Starting to code a robust routine to add two arrays element by element $new = $a + $b ... I thought that this should already be done and searched CPAN for a sutiable module. Despite Math:::Matrix I could not realy find much and the ::Matrix module seemed to be more that I needed...
6
2404
by: Jamie Fryatt | last post by:
Hi everyone, here's what id like to do. I have a table with 2 fields, name and value I need to be able to add multiple records quickly, for example I need to add name value abc 1 abc 2 abc 3
34
4160
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and "push_back" a copy of this into a vector V. This is repeated many times in an iterative process. Ok whenever I "push_back" a copy of Class A, I also want to assign a pointer contained in an exisiting instance of a Class B to this
23
31078
by: Rick | last post by:
Hi, This is probably simple byt when you never did pointers and being used to luxery strings like in Delphi or Visual Basic, C can get though. What I'm trying to do is to add chars to a string. I looked in the string.h file but I didn't find that kind of function (that string library is more than 7 years old) so I decided to write that function on myself. It seems to work although when I'm trying to do printf's between those actions I...
5
15482
by: Troy | last post by:
Hello, I have a dumb question. I have two array objects of type double with 12 elements in each object. I'd like to add the two objects but dont know how to. Any ideas? thanks
6
10793
by: Maheshkumar.R | last post by:
How i can store images in array @ runtime..? What are the possbile ways.. Thankz.. - Mähésh Kumär. R
1
1976
by: mtchatchez | last post by:
I have several 2-dimensional arrays which I want to sum the values of, returning an array that has for each of its values the sum from all the others (if that makes sense?) ... Okay ... maybe an example will make it clearer! ;-) $array1 = array(array(1,1,1), array(1,1,1), array(1,1,1)); $array2 = array(array(2,2,2), array(0,0,0), array(3,3,3)); ... So is there a clever / fast way to add up the values of the two arrays above to return the...
6
6553
by: santiago | last post by:
I guess one cannot do this: arraytot = arraytot + arraydet; So, what's the trick to adding arrays like this? Thanks.
0
8272
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8205
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8644
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8370
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8514
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4208
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2632
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.