In PHP, array_filter and array_reduce are both powerful functions for processing arrays, but their uses and how they work are significantly different. Understanding the differences and usage scenarios between these two functions will help to write more concise and efficient code.
array_filter is used to filter array elements. It checks each element through a callback function. The element with the return value of true will be retained, and the element with the return false will be removed. Simply put, it is to filter out array elements that meet the requirements based on the conditions.
<?php
$arr = [1, 2, 3, 4, 5, 6];
// Filter out all even numbers
$result = array_filter($arr, function($value) {
return $value % 2 === 0;
});
print_r($result);
?>
Output result:
Array ( [1] => 2 [3] => 4 [5] => 6 )
Note: array_filter does not change the key name of the element.
array_reduce is used to reduce an array into a single value. It merges array elements into a cumulative value through a callback function, which is suitable for operations such as accumulating, summing, and splicing strings.
<?php
$arr = [1, 2, 3, 4, 5];
// Calculate the sum of all elements of the array
$sum = array_reduce($arr, function($carry, $item) {
return $carry + $item;
}, 0);
echo $sum;
?>
Output result:
15
$carry is the cumulative value, the callback will keep updating it, and the final return is the reduction result.
Features | array_filter | array_reduce |
---|---|---|
effect | Filter the array and return a subset that meets the criteria | "Reduce" the array to a single value |
Return value | An array | A scalar or object |
Main uses | Conditional filter elements | Aggregation, merge, accumulation |
Reserved key names | reserve | No key names are involved, the accumulation of values is concerned |
When you need to filter out elements that meet the criteria from the array , use array_filter . For example: filter out all users older than 18 years old.
When you need to convert an array into a value , use array_reduce . For example: calculate the total score, splice strings, and generate statistical data.
Suppose there is an order array containing multiple order amounts.
<?php
$orders = [120, 250, 90, 300, 60];
// The filtered amount is greater than100Orders
$filteredOrders = array_filter($orders, function($amount) {
return $amount > 100;
});
// 计算筛选后Orders总金额
$total = array_reduce($filteredOrders, function($carry, $amount) {
return $carry + $amount;
}, 0);
echo "Greater than100Orders总金额是:" . $total;
?>
Through the above example, it can be clearly seen that array_filter first filters out orders that meet the conditions, and then uses array_reduce to sum. When both are used together, the array data can be processed flexibly.
array_filter focuses on filtering elements , retaining array items that meet the criteria.
array_reduce focuses on reduction merges , "compressing" the array into a result.
Choose according to your needs or use in combination, which can make the code more elegant and efficient.