note 17833 added to function.array-sum
| From: | coyote at i-hwy dot net | Date: | Thu, 27 Dec 2001 23:05:32 +0000 |
| Subject: | note 17833 added to function.array-sum | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-23892@lists.php.net to get a copy of this message | ||
A warning... I am using php 4.04pl1 and whenever I use the array sum function on an array containing
both string and numeric values, it does the sum of the numeric values properly, but turns all
non-number values in the array into the value "0" (zero).
I tried creating a function that copied the array into another variable so that it would not be
operating on the original... but mysteriously, it still destroyed the string values in the original
array! Making a copy of the array into a new variable before passing it to the function seemed to
work okay, but I didn't want to do that for my particular operation.
My workaround was to create a function to use in place of sum_array. This is, of course, quick and
dirty.
function num_array_sum($the_array) {
$num = count($the_array);
$y=0;
for ($i = 0; $i < $num; $i++) {
$y = $y + $the_array[$i];
}
return $y;
}
--
http://www.php.net/manual/en/function.array-sum.php
http://master.php.net/manage/user-notes.php?action=edit+17833
http://master.php.net/manage/user-notes.php?action=delete+17833
http://master.php.net/manage/user-notes.php?action=reject+17833