Home >Backend Development >PHP Tutorial >How to use array_sum() to calculate the sum of array element values

How to use array_sum() to calculate the sum of array element values

php中世界最好的语言
php中世界最好的语言Original
2018-05-21 10:35:157279browse

This time I will show you how to use array_sum() to calculate the sum of the array element values, and how to use array_sum() to calculate the sum of the array element values.Notes What are they? Here are actual cases. Let’s take a look.

array_sum() definition and usage
array_sum() Function Returns the sum of all values ​​in the array.

If all values ​​are integers, an integer value is returned. If one or more of the values ​​is a floating point number, a floating point number is returned.

Versions prior to PHP 4.2.1 modified the incoming array itself, converting the string values ​​into numeric values ​​(converted to zero in most cases, depending on the specific system) depends).

Syntax
array_sum(array)
Parameter Description
array Required. Specifies the input array.

The above are five examples of the array_sum function for your reference.

<?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

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

array_search() function returns the key name by element value Detailed explanation of the steps

php array search function usage Summary of methods

The above is the detailed content of How to use array_sum() to calculate the sum of array element values. 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