Creating an array of numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wbryan6
Forum Newbie
Posts: 22
Joined: Sat Feb 04, 2006 12:13 pm

Creating an array of numbers

Post by wbryan6 »

I have been working on a page that sums up numbers from an array, but the problem I am having is that my array length varies depending on the number of results returned from the database. Essentially is:

Code: Select all

$z = 0;
while(z < $count_from_db)
{
      $array_of_num[] = $z;
      $z++;
}

$to_sum = array($array_of_num);
At the moment I have to enter in values for $to_sum, ie

Code: Select all

$to_sum = array('0','1','2');
Is it possible to have the list of values I need generated into the code? Hopefully this description is thorough enough. Thanks.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

MySQL's SUM function may be of some use...

Code: Select all

SELECT SUM(col_to_be_summed) AS the_sum FROM table
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Or array_sum() may be worth looking up.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't quite understand what ~wbryan6 is asking for.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Me neither.
The Sum of 1+2+3...+n is n(n+1)/2 and does not require array_sum ;)
Post Reply