Home  >  Article  >  Backend Development  >  php function array_sum() that returns the sum of all values ​​in the array

php function array_sum() that returns the sum of all values ​​in the array

黄舟
黄舟Original
2017-11-09 09:10:172022browse

Example

Return the sum (5+15+25) of all values ​​in array:

<?php
$a=array(5,15,25);
echo array_sum($a);
?>

Definition and usage

array_sum() FunctionReturns the sum of all values ​​in the array.

Syntax

array_sum(array)
Parameters Description
array Required. Specifies an array.

Technical details

# Versions prior to ##PHP 4.2.1 modified the passed array itself, converting the
Return value: Returns the sum of all values ​​in the array.
PHP Version: 4.0.4+
Change Log: string values ​​in it to numeric values ​​(in most cases to zero, depending on the value).
More examples

Example 1

Return the sum of all values ​​in the array (52.2+13.7+0.9):

<?php
$a=array("a"=>52.2,"b"=>13.7,"c"=>0.9);
echo array_sum($a);
?>

Example 1

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

Output:


45

Example 2

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

Output:


45

Example 3

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

Output:


45.5

Example 4

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

Output:


45

Example 5

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

Output:


30


The above is the detailed content of php function array_sum() that returns the sum of all values ​​in the array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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