You are here: Re: Adding arrays « PHP Programming Language « IT news, forums, messages
Re: Adding arrays

Posted by Daz on 05/07/07 16:12

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...@gmail.com> wrote:
>
> > 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 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++) {
> > 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.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация