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

黄舟
Release: 2023-03-17 08:44:01
Original
2063 people have browsed it

Example

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

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

Definition and usage

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

Syntax

array_sum(array)
Copy after login
ParametersDescription
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);
?>
Copy after login

Example 1

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

Output:


45
Copy after login
Copy after login
Copy after login

Example 2

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

Output:


45
Copy after login
Copy after login
Copy after login

Example 3

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

Output:


45.5
Copy after login

Example 4

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

Output:


45
Copy after login
Copy after login
Copy after login

Example 5

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

Output:


30
Copy after login


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!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template