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
Return value: | Returns the sum of all values in the array. |
PHP Version: | 4.0.4+ |
Change Log: | # Versions prior to ##PHP 4.2.1 modified the passed array itself, converting thestring values in it to numeric values (in most cases to zero, depending on the value). |
<?php $a=array("a"=>52.2,"b"=>13.7,"c"=>0.9); echo array_sum($a); ?>
<?php $a=array(0=>"5",1=>"15",2=>"25"); echo array_sum($a); ?>
45
<?php $a=array(0=>5,1=>15,2=>25); echo array_sum($a); ?>
45
<?php $a=array(0=>5,1=>15.5,2=>25); echo array_sum($a); ?>
45.5
<?php $a=array(0=>5,1=>"15s",2=>25); echo array_sum($a); ?>
45
<?php $a=array(0=>5,1=>"s15s",2=>25); echo array_sum($a); ?>
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!