Re: Adding arrays



On May 7, 1:21 pm, Rami Elomaa <rami.elo...@xxxxxxxxx> wrote:
ZeldorBlat kirjoitti:



On May 7, 11:05 am, dennis.spreng...@xxxxxxxxx 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 multidimensional 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_cumulative($arr) {
$out = array();

for($i = 0; $i < count($arr); $i++) {

This is hair-splitting, but since count($arr) is static, it would save
some cpu cycles to store it in a variable instead of calling count() on
each iteration:

for($i = 0, $limit=count($arr); $i < $limit ; $i++) {

Or better yet, iterate the array with foreach:

foreach($arr as $i => $row) {

if(!isset($out[$i]))
$out[$i] = array();

for($j = 0; $j < count($arr[$i]); $j++)

Same goes here.

I've gotten so used to foreach that I'm missing it a lot when I'm using
a language that hasn't got such a structure... It's so convinient.

--
Rami.Elo...@xxxxxxxxx

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

Yes -- you're correct on both. In the normal course of things I would
save the value of count() and also use a foreach instead. Using
count() over and over again makes it a bit easier to follow the code,
and the OP was using regular for loops so I stuck with those.

In practice I use foreach almost exclusively -- and you're right in
that I really miss it in other languages :)

.



Relevant Pages

  • Re: Loops performance contradictions.
    ... how do you set the data using foreach??? ... You're just writing to the array in a tight loop. ... >> happen on each iteration. ... >> Set the array data in about 3438380 ticks. ...
    (microsoft.public.dotnet.framework.performance)
  • Re: [PHP] foreach() using current() strange beahvior
    ... that's expected as foreach moves the internal array pointer, ... When using the internal pointer just by calling current(so not moving ... Unless the array is referenced, foreach operates on a copy of the ...
    (php.general)
  • Re: a question about for and foreach
    ... foreach (@array) { ... why @array are changed after this foreach loop!!! ... The "foreach" loop iterates over a normal list value and sets the ... If any element of LIST is an lvalue, you can modify it by modifying ...
    (perl.beginners)
  • Re: safe to delete elements of array in foreach
    ... I agree with Jon on this one. ... I make it a habit not to delete entries in a foreach() loop. ... build an array of keys I want to delete, and after the loop ends, delete ... I don't know whether an operation like this is guaranteed to work in PHP ...
    (comp.lang.php)
  • Re: [PHP] Foreach
    ... The problem I am having is getting both to update the table in MySQL. ... echo "$t"; ... foreach { ... // foreach expects an array, ...
    (php.general)