array_map() applies a function to every element and returns a new array of the results, array_filter() keeps only the elements for which a callback returns true, and array_reduce() folds all elements into a single value. Together, php array_map array_filter and array_reduce let you transform data without writing manual loops.
None of the three modifies the original array. The examples use PHP 8.3 and arrow functions, which have existed since PHP 7.4.
array_map()
array_filter()
array_reduce()array_map()array_map() takes a callback and an array, runs the callback on each value and returns the new array. Keys are preserved when you pass exactly one array.
The arrow function fn captures variables from the surrounding scope by value automatically, so you can use an outer $rate inside it without a use clause.
Pass more than one array and the callback receives one element from each. The result is indexed from zero, and shorter arrays are padded with null.
With a single array, string keys are kept. With several arrays, keys are lost. To map values and keep keys on an associative array, array_map() on one array is enough. To also change keys, use array_combine() or a foreach.
array_filter()array_filter() returns the elements for which the callback is truthy. Keys are preserved, so you often get gaps.
Wrap the result in array_values() when you need a clean list, especially before json_encode(), otherwise the result becomes a JSON object.
With no callback, array_filter() removes every falsy value: 0, '0', '', null, false and []. That is handy for dropping empty strings but dangerous if 0 is a valid value.
The third argument changes what the callback receives. ARRAY_FILTER_USE_KEY passes only the key, and ARRAY_FILTER_USE_BOTH passes the value and then the key.
array_reduce()array_reduce() carries an accumulator through the array. The callback gets the accumulator and the current element and returns the next accumulator. The third argument is the starting value.
Always give an initial value. Without one, the start is null, and an empty array returns null instead of a sensible zero.
You can pass the result of one function into the next to build a readable pipeline, reading from the inside out.
For a pipeline this short a single foreach is also fine. Prefer the functional form when each step is a clear idea, and prefer a loop when you need early exit or to build several results at once.
| Need | Better choice |
|---|---|
| Transform every value | array_map() |
| Keep a subset | array_filter() |
| Collapse to one value | array_reduce() |
Stop early with break | foreach |
| Large data, low memory | generators with foreach |
These functions build full arrays in memory. For very large data, a generator avoids holding every intermediate result.
array_map() and array_filter()?It is a historical inconsistency. array_map() takes the callback first, while array_filter() and array_reduce() take the array first. Check the signature when in doubt.
array_map() change the original array?No. It returns a new array. If you want to modify elements in place, use foreach with a reference or array_walk().
array_filter() leave gaps in the keys?It preserves the original keys of the elements it keeps. Call array_values() to renumber them.
Yes. Pass [$object, 'method'], 'ClassName::method', or in PHP 8.1 and later the first-class callable syntax $object->method(...) or strtoupper(...).
Use array_map() to transform each element, array_filter() to keep the ones you want and array_reduce() to boil an array down to one value. Remember that filter keeps keys, reduce needs an initial value, and map with several arrays loses string keys. Arrow functions keep the callbacks short, and a plain loop is still the right tool when you need early exit.