Master PHP Array Functions: array_filter, array_map, array_walk & array_reduce
This guide explains how PHP's array_filter, array_map, array_walk, and array_reduce functions work, showing code examples that replace manual loops, improve performance, and simplify common array operations for backend developers.
array_filter()
The array_filter() function iterates over an array and keeps only the elements for which the supplied callback returns a truthy value. Keys are preserved by default. If no callback is provided, all values that evaluate to true are retained. An optional third argument ( ARRAY_FILTER_USE_KEY or ARRAY_FILTER_USE_BOTH) can be used to pass the key or both key and value to the callback.
$arr = ['a', 0, 'b', 'c', 1, 2, 3, 4, 5, 6, 7];
// Keep only string values
$new_arr = array_filter($arr, function ($v) {
return is_string($v);
});
print_r($new_arr);
// Output:
// Array ( [0] => a [2] => b [3] => c )array_map()
array_map()applies a callback to each element of one or more arrays and returns a new array containing the results. The callback receives the corresponding values from each input array in the order they are passed. If the arrays have different lengths, array_map() stops at the shortest one.
$arr1 = [1, 2, 3, 4, 5];
$arr2 = [6, 7, 8, 9, 10, 55];
$new_arr = array_map(function ($val1, $val2) {
return $val1 + $val2;
}, $arr1, $arr2);
var_dump($new_arr);
// Output:
// array(6) {
// [0]=> int(7)
// [1]=> int(9)
// [2]=> int(11)
// [3]=> int(13)
// [4]=> int(15)
// [5]=> int(55)
// }array_walk()
array_walk()traverses an array and invokes a user‑defined callback for each element. The callback receives the value and the key; the value can be modified by reference, allowing in‑place changes to the original array.
$arr = ['a', 0, 'b', 'c', 1, 2, 3, 4, 5, 6, 7];
array_walk($arr, function (&$val, $key) {
// Append the string "_x" to each element for demonstration
$val = $val . '_x';
});
var_dump($arr);
// Example output (truncated):
// array(11) {
// [0]=> string(3) "a_x"
// [1]=> string(2) "0_x"
// ...
// }array_reduce()
array_reduce()collapses an array to a single scalar value by repeatedly applying a callback. The callback receives the accumulator (carry) and the current item, and must return the updated accumulator. An optional third argument supplies the initial value of the accumulator.
$array = [10, 15, 20, 60];
$sum = array_reduce($array, function ($carry, $item) {
return $carry + $item;
}, 50); // start with 50
print_r('Total: ' . $sum);
// Output: Total: 155
// Equivalent built‑in function
print_r(array_sum($array)); // 105These built‑in array functions enable developers to replace explicit for / foreach loops with concise, functional‑style code, often improving readability and performance.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
