php数组函数序列之array_sum() - 计算数组元素值之和_PHP教程

WBOY
Release: 2016-07-21 15:23:33
Original
1118 people have browsed it

array_sum()定义和用法
array_sum() 函数返回数组中所有值的总和。

如果所有值都是整数,则返回一个整数值。如果其中有一个或多个值是浮点数,则返回浮点数。

PHP 4.2.1 之前的版本修改了传入的数组本身,将其中的字符串值转换成数值(大多数情况下都转换成了零,根据具体制而定)。

语法
array_sum(array)
参数 描述
array 必需。规定输入的数组。

例子1

复制代码代码如下:

$a=array(0=>"5",1=>"15",2=>"25");
echo array_sum($a);
?>

输出:

45
例子2
复制代码代码如下:

$a=array(0=>5,1=>15,2=>25);
echo array_sum($a);
?>

输出:

45
例子3
复制代码代码如下:

$a=array(0=>5,1=>15.5,2=>25);
echo array_sum($a);
?>

输出:

45.5
例子4
复制代码代码如下:

$a=array(0=>5,1=>"15s",2=>25);
echo array_sum($a);
?>

输出:

45
例子5
复制代码代码如下:

$a=array(0=>5,1=>"s15s",2=>25);
echo array_sum($a);
?>

输出:

30

www.bkjia.com true http://www.bkjia.com/PHPjc/324490.html TechArticle array_sum()定义和用法 array_sum() 函数返回数组中所有值的总和。 如果所有值都是整数,则返回一个整数值。如果其中有一个或多个值是浮点数...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!