In PHP, you can use the array_sum() function to calculate the sum of all values in an array.
array_sum() The syntax of the function is as follows:
array_sum(array $array): float|int
Among them, $array is the array to calculate the sum. The function returns a float or integer representing the sum of all values in $array.
The following is a sample code that shows how to use the array_sum() function to calculate the sum of an array containing numbers:
$numbers = array(1, 2, 3, 4, 5); $sum = array_sum($numbers); echo "数组中所有数字的总和为:" . $sum;
The output of the above code will be:
数组中所有数字的总和为:15
If the array contains non-numeric elements, these elements will be ignored and will not affect the calculation results.
If the array has no elements, the function will return 0.
In actual development, we may need to sum the values of a field in a two-dimensional array. At this time, you can use the array_column() function to extract the value of the field, and then apply the array_sum() function to the extracted array.
The following is a sample code that shows how to use the array_column() and array_sum() functions to calculate the sum of the values of a field in a two-dimensional array:
$data = array( array("name" => "张三", "age" => 20, "score" => 80), array("name" => "李四", "age" => 21, "score" => 85), array("name" => "王五", "age" => 22, "score" => 90), ); $scores = array_column($data, "score"); $total_score = array_sum($scores); echo "学生的总分数为:" . $total_score;
The result of the above code output Will be:
学生的总分数为:255
In actual development, we can also sum certain values in an associative array. At this time, you can use the array_reduce() function to customize an accumulator function, and then apply this function to accumulate the values in the array.
The following is a sample code that shows how to use the array_reduce() function to accumulate certain values in an associative array:
$data = array( "apple" => 3.00, "orange" => 4.50, "banana" => 2.00, ); $total_cost = array_reduce($data, function($carry, $value) { return $carry += $value; }); echo "购物车中所有水果的总价为:" . $total_cost;
The output of the above code will be:
购物车中所有水果的总价为:9.5
In the above code, the array_reduce() function receives an array and a custom accumulator function as parameters. The $carry parameter in the accumulator function represents the temporary result of accumulation, and the $value parameter represents the current array element value. The function returns the updated cumulative result. The final accumulated result is used as the return value of the array_reduce() function. In this example, the logic of the accumulator function is very simple, which is to implement the addition operation of adding two numbers.
To sum up, whether it is a one-dimensional array, a two-dimensional array or an associative array, PHP provides various array functions to facilitate data processing. In actual development, we must flexibly use these functions to improve the efficiency and readability of the code.
The above is the detailed content of What is the function to find the sum of arrays in php?. For more information, please follow other related articles on the PHP Chinese website!