array_map($callback, $array) calls $callback on every element of $array and returns a new array with the results. The callback comes first, the array second, and the original array is left unchanged.
fn($p) => $p * 1.2 is an arrow function: a one-expression function that returns its result. Any callable works in its place.
Use a built-in function as the callback
Pass the name of a built-in function as a string. This is the shortest way to clean up a whole array: trim every value, convert strings to integers, or change case.
array_map('intval', ...) is the usual way to turn the strings from a form or explode() into real integers. You can also write intval(...), the first-class callable syntax from PHP 8.1, which your editor can autocomplete and check.
Use a closure with outside variables
An arrow function sees the variables around it automatically. A classic function needs use to bring them in. Both are explained on the anonymous functions page.
array_map with keys
With a single array, array_map keeps the keys, including string keys. But the callback only receives the value. To get the key too, pass array_keys($array) as a second array: the callback is then called with one element from each array.
Notice the second result: as soon as you pass more than one array, the keys are replaced by 0, 1, 2. That is why the last line uses array_combine() to rebuild them.
Map multiple arrays at once
Every extra array becomes an extra argument to the callback, matched by position. If the arrays have different lengths, the shorter ones are padded with null.
Passing null as the callback skips the function and zips the arrays into pairs:
array_map vs array_walk vs foreach
array_map builds a new array. array_walk changes the array you pass in, through a reference, and gives the callback the key as its second argument. A foreach does either and can also skip elements or stop early with break.
Use array_map when every element turns into exactly one new element. If you also need to drop some elements, combine it with array_filter, or write a foreach.
Common mistake: the argument order
array_map takes the callback first and the array second. Its neighbour array_filter takes them the other way round, which is easy to mix up:
array_map($callback, $array); // callback first
array_filter($array, $callback); // array first
Swapping them makes PHP 8 throw a TypeError, because a plain array is not a valid callback:
PHP stops with:
PHP Fatal error: Uncaught TypeError: array_map(): Argument #1 ($callback) must be a valid callback or null, array callback must have exactly two members in /home/index.php:4
Swap the two arguments back and the block runs.
Frequently Asked Questions
What does array_map do in PHP?
It calls a function on every element of an array and returns a new array with the results: array_map(fn($n) => $n * 2, [1, 2, 3]) returns [2, 4, 6]. The original array is not changed.
Does array_map keep the keys?
With one array, yes: string and integer keys are kept as they are. With two or more arrays, the result always gets new keys 0, 1, 2....
How do I get the key inside the array_map callback?
Pass the keys as a second array: array_map(fn($k, $v) => "$k=$v", array_keys($arr), $arr). The result is a list, so wrap it in array_combine(array_keys($arr), ...) if you need the original keys back.
Is array_map faster than foreach?
Not meaningfully. Both loop over the array once, and a plain foreach is often slightly faster because it avoids a function call per element. Choose array_map when it reads better: one transformation that produces a new array.
What is the difference between array_map and array_walk?
array_map returns a new array and leaves the input alone. array_walk returns true and is meant to change the array in place through a reference parameter: array_walk($arr, function (&$v, $k) { $v = trim($v); });.