Home > Backend Development > PHP Problem > What is the function to find the sum of arrays in php?

What is the function to find the sum of arrays in php?

PHPz
Release: 2023-04-24 16:37:11
Original
534 people have browsed it

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;
Copy after login

The output of the above code will be:

数组中所有数字的总和为:15
Copy after login

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;
Copy after login

The result of the above code output Will be:

学生的总分数为:255
Copy after login

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;
Copy after login

The output of the above code will be:

购物车中所有水果的总价为:9.5
Copy after login

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!

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