Home  >  Article  >  Backend Development  >  What is the php array conditional sum function?

What is the php array conditional sum function?

PHPz
PHPzOriginal
2023-04-19 11:37:09666browse

PHP is a programming language widely used in web development. In PHP, arrays are a very common data type with many useful functions. Among them, the array conditional sum function is a very practical function that can help us sum the elements in the array that meet specific conditions.

Array conditional summation functions are usually divided into two types: one is to sum all elements in the array, and the other is to sum the elements in the array that meet specific conditions. For the former case, we can use PHP's built-in array_sum() function to achieve this. The use of this function is very simple. You only need to pass in an array as a parameter and it will return the sum of all elements in the array.

For example, we create a simple array:

$numbers = array(1, 2, 3, 4, 5);

Then we can use the array_sum() function to find the sum of all elements of the array:

$total = array_sum($numbers);
echo $total;

The output result is 15 , that is, the sum of 1 2 3 4 5.

For the second case, that is, to sum the elements in the array that meet specific conditions, we can use the array_reduce() function in PHP. This function needs to pass in two parameters: the first parameter is the array to be calculated, and the second parameter is a callback function. The function of the callback function is to calculate each array element and return the calculated result.

For example, we have an array that stores product prices, and we want to sum the products whose prices are greater than 100. We can achieve this:

$prices = array(50, 150, 200, 80, 120);
$total = array_reduce($prices, function($carry, $item) {
    if ($item > 100) {
        return $carry + $item;
    } else {
        return $carry;
    }
});

echo $total;

The output result is 470, which is the sum of 150 200 120. In this example, we use an anonymous function as the callback function. This anonymous function determines whether each array element is greater than 100. If so, the element is added to the $carry variable, otherwise no operation is performed. Finally, the $total variable is the sum of elements that meet the condition.

In addition to using the callback function, we can also process the array before calling the array_reduce() function, for example, use the array_filter() function to filter the array, and then use the array_reduce() function to sum:

$prices = array(50, 150, 200, 80, 120);
$filtered_prices = array_filter($prices, function($item) {
    return $item > 100;
});
$total = array_reduce($filtered_prices, function($carry, $item) {
    return $carry + $item;
});

echo $total;

The output is the same as the previous example.

To sum up, the array conditional summation function in PHP is a very practical function that can help us quickly calculate the elements in the array. Whether you are summing the entire array or summing the elements that meet specific conditions, you can use PHP's built-in functions to achieve it. At the same time, by mastering the use of these functions, we can also improve the efficiency and code quality in PHP development.

The above is the detailed content of What is the php array conditional sum function?. 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