Home > Article > Backend Development > How to find the sum of array elements in php
Method: 1. Use array_sum() to calculate the sum of all elements in the array. The syntax is "array_sum($arr)"; 2. Use the for statement to loop through the array and add the elements one by one. , the syntax is "for($i=0;$i
The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer
PHP provides multiple methods to add array elements Sum, three types are introduced below.
Method 1: Use array_sum() function
array_sum() is a built-in function in PHP that can calculate the value of all elements in an array and , and returns the sum of the elements.
<?php header("Content-type:text/html;charset=utf-8"); $array= array(1,2,3,4,5,6,7,8,9,10); var_dump($array); echo '数组所有元素之和:'. array_sum($array); ?>
Description:
If all elements in $array are integers, return an integer value; if there is one or more values are floating point numbers, a floating point number is returned.
If there are non-numeric elements in $array, PHP will convert them into a numeric value (PHP is a weak language type and will automatically convert the variable into Correct data type), if the conversion fails, it will be used as a value of 0 to participate in the calculation.
<?php header("Content-type:text/html;charset=utf-8"); $array= array("10.1xy", 100, '1', "0.01"); var_dump($array); echo '数组所有元素之和:'. array_sum($array); ?>
Method 2: Use the for statement to loop through the array and add the elements one by one
<?php $array= array(1,2,3,4,5,6,7,8,9,10); $sum=0; $len=count($array);//数组长度 for ($i=0; $i < $len; $i++) { $sum+=$array[$i]; } echo '1 + 2 + 3 +...+ 9 + 10 = '. $sum; ?>
Note: The for statement is only valid for index arrays and cannot traverse associated arrays
The for loop will control the loop The variable of times is predefined in the for statement, so the for loop statement can perform loop operations according to the known number of loops, which is suitable for situations where the number of times the script needs to be run is clearly known (based on the numerical subscript of the index array).
Method 3: Use foreach to loop through the array and add the elements one by one.
foreach is specially designed for traversing arrays Statement is a commonly used method when traversing arrays, which provides great convenience in traversing arrays; after PHP5, objects can also be traversed (foreach can only be applied to arrays and objects).
The foreach statement traverses the array regardless of the array subscript, and can be used for discontinuous index arrays and associative arrays with strings as subscripts.
<?php header('content-type:text/html;charset=utf-8'); $array= array(1,2,3,4,5,6,7,8,9,10); $sum=0; foreach ($array as $value) { $sum+=$value; } echo '数组所有元素之和:'. $sum; ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to find the sum of array elements in php. For more information, please follow other related articles on the PHP Chinese website!