php sum array

PHPz
Release: 2023-05-19 14:07:07
Original
580 people have browsed it

In PHP, array is a very useful data structure. It can be used to store a set of related data and make data manipulation more convenient. The elements in the array can be of any type, including numbers, strings, objects, etc.

When we need to sum all elements in an array, we can use PHP's built-in function array_sum(). This function accepts an array as a parameter and returns the result of adding all elements of the array.

The following is an example showing how to use the array_sum() function to sum an array:

//定义一个数组
$num_arr = array(1, 2, 3, 4, 5);

//使用array_sum()函数求和
$sum = array_sum($num_arr);

//输出结果
echo $sum; //输出15
Copy after login

In the above code, we first define an array $num_arr containing 5 integer elements. Then, we use PHP's built-in array_sum() function to sum the array and assign the result to the variable $sum. Finally, we output the value of $sum, which is 15.

It should be noted that the array_sum() function can only be used for summation operations. If the element in the array is not of numeric type, it will be converted to 0.

//定义一个数组
$str_arr = array('a', 'b', 'c');

//使用array_sum()函数求和
$sum = array_sum($str_arr);

//输出结果
echo $sum; //输出0
Copy after login

In the above code, we define an array $str_arr containing 3 string elements. Since these strings cannot be converted numerically, they are converted to 0 when the array_sum() function is called. Therefore, the final output $sum value is 0.

In addition to the array_sum() function, we can also use the method of looping through the array to sum it. The following is an example of using a for loop to implement array summation:

//定义一个数组
$num_arr = array(1, 2, 3, 4, 5);

//使用for循环对数组求和
$sum = 0;
for ($i = 0; $i < count($num_arr); $i++) {
    $sum += $num_arr[$i];
}

//输出结果
echo $sum; //输出15
Copy after login

In the above code, we use a for loop to traverse the array $num_arr, and use the variable $sum to save the cumulative result. In each loop, we add the value of the current element to $sum. Ultimately, the value of $sum is the sum of all elements in the array.

In general, PHP arrays are very powerful and flexible. It provides many convenient functions and methods to help us operate arrays more efficiently. Using the array_sum() function to sum arrays can make the code more concise and readable, improve development efficiency, and reduce the error rate of the code to a certain extent.

The above is the detailed content of php sum array. For more information, please follow other related articles on the PHP Chinese website!

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