Home>Article>Backend Development> 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:15 7196browse

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

array_sum() definition and usage
array_sum()FunctionReturns the sum of all values in the array.

If all values areintegers, 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 thestringvalues 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.

"5",1=>"15",2=>"25"); echo array_sum($a); ?> 输出: 45
5,1=>15,2=>25); echo array_sum($a); ?> 输出: 45
5,1=>15.5,2=>25); echo array_sum($a); ?> 输出: 45.5
5,1=>"15s",2=>25); echo array_sum($a); ?> 输出: 45
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