(PHP 4 >= 4.0.4, PHP 5)
array_sum — 计算数组中所有值的和
array_sum() 将数组中的所有值的和以整数或浮点数的结果返回。
Example #1 array_sum() 例子
<?php
$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum($a) . "\n";
$b = array("a" => 1.2, "b" => 2.3, "c" => 3.4);
echo "sum(b) = " . array_sum($b) . "\n";
?>
以上例程会输出:
sum(a) = 20 sum(b) = 6.9
Note:
PHP 4.2.1 之前的版本修改了传入的数组本身,将其中的字符串值转换成数值(大多数情况下都转换成了零,根据具体值而定)。
array
The input array.
Returns the sum of values as an integer or float.
版本 | 说明 |
---|---|
4.2.1 | PHP versions prior to 4.2.1 modified the passed array itself and converted strings to numbers (which most of the time converted them to zero, depending on their value). |
Example #2 array_sum() examples
<?php
$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum($a) . "\n";
$b = array("a" => 1.2, "b" => 2.3, "c" => 3.4);
echo "sum(b) = " . array_sum($b) . "\n";
?>
以上例程会输出:
sum(a) = 20 sum(b) = 6.9