Re: Sum from Arrays ?

From: Date: Wed, 07 Feb 2001 17:51:12 +0000
Subject: Re: Sum from Arrays ?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-38577@lists.php.net to get a copy of this message
As of PHP 4.0.4 the function array_sum() exists (it's undocumented, but in changelog), it's used as such : $array = array(1,3,5); $sum = array_sum($array); Or loop through array as such : foreach ($array as $value) { $sum += $value; } Either way, $sum now equals 9. For info on how all the above works, see : http://www.php.net/manual/en/control-structures.foreach.php http://php.net/ChangeLog-4.php http://www.php.net/manual/en/language.operators.assignment.php Regards, Philip Olson http://www.cornado.com/ On Wed, 7 Feb 2001, Keith Whyman wrote: > No I've explained this badly > I've got the array $a what I want is a sum of what's in the array > ie from your sample the answer would be 1+3+5 = 9 > > Thanks > Keith > > > > Hum, maybe the count() function. Works with mysql_fetch_array() too. > Here's > > a sample: > > $a[0] = 1; > > $a[1] = 3; > > $a[2] = 5; > > $result = count ($a); > > //$result == 3 > > > > Here's a link: > > http://php.net/manual/en/function.count.php > > DISCLAIMER: I am by no means well-versed in this, or any other, topic.... > > - Shane > > > > ----- Original Message ----- > > From: "Keith Whyman" <k.whyman@bf-72.de> > > To: <php-general@lists.php.net> > > Sent: Wednesday, February 07, 2001 12:15 PM > > Subject: [PHP] Sum from Arrays ? > > > > > > > It's probably a stupid question but........... > > > I've got the following ; > > > > > > SELECT t_category_name COUNT(*) AS count > > > FROM t_city,t_zipcodecity,t_location,t_category , t_locationcategory > > > WHERE (t_category_id_category = t_locationcategory_id_category) > > > AND (t_location_id_location = t_locationcategory_id_location) > > > AND (t_zipcodecity_zipcode = t_location_zipcode) > > > AND (t_city_id_city = t_zipcodecity_id_city) > > > AND (t_city_id_city = 1) > > > AND ((t_location_sex = 4) OR (t_location_sex = 0)) > > > GROUP BY t_category_name"; > > > > > > $result1 = mysql_db_query(DBWEB, $query); > > > print mysql_error(); > > > echo " > > > <TABLE width=\"100%\"> > > > "; > > > if ($result1) { > > > > > > > > > while ($r = mysql_fetch_array($result1)) { > > > $numbers = $r["count"]; > > > $names =$r["$t_category_name"]; > > > > > > And what I want is a total for my count - I think it something to do > with > > > the mySQL sum funktion but I've got a bit stuck ! > > > > > > > > > > > > -- > > > PHP General Mailing List (http://www.php.net/) > > > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > > > For additional commands, e-mail: php-general-help@lists.php.net > > > To contact the list administrators, e-mail: php-list-admin@lists.php.net > > > > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#38577) next »