What does array_map() do?
array_map() calls $callback once for each element and collects the return values in a new array; the input array is not modified. With a single array, keys are preserved. With several arrays, the callback receives one element from each array per call and the result is re-indexed.
Syntax
array_map(?callable $callback, array $array, array ...$arrays): array
Parameters
$callback- A callable (closure, arrow functionfn($x) => ..., or function name such as'trim'). Passnullto "zip" several arrays together.$array- The array to map over....$arrays- Optional extra arrays. The callback must accept as many parameters as arrays passed. Shorter arrays are padded withnull.
Return value
A new array containing the callback results. Keys are preserved only when exactly one array is passed.
array_map() examples
Every example below shows the exact output it produces. Paste any of them into the editor above (or the online compiler) to run it.
1. Square every number with an arrow function
<?php
$numbers = [1, 2, 3, 4, 5];
$squared = array_map(fn($n) => $n * $n, $numbers);
print_r($squared);
Output:
Array
(
[0] => 1
[1] => 4
[2] => 9
[3] => 16
[4] => 25
)
2. Built-in function names and preserved keys
<?php
$names = ["alice", "bob", "carol"];
print_r(array_map('ucfirst', $names));
$prices = ["apple" => 1.2, "pear" => 0.8];
$withTax = array_map(fn($p) => round($p * 1.21, 2), $prices);
print_r($withTax);
Output:
Array
(
[0] => Alice
[1] => Bob
[2] => Carol
)
Array
(
[apple] => 1.45
[pear] => 0.97
)
3. Multiple arrays and a null callback
<?php
$first = ["John", "Jane"];
$last = ["Doe", "Smith"];
print_r(array_map(fn($f, $l) => "$f $l", $first, $last));
print_r(array_map(null, [1, 2], ["a", "b"]));
Output:
Array
(
[0] => John Doe
[1] => Jane Smith
)
Array
(
[0] => Array
(
[0] => 1
[1] => a
)
[1] => Array
(
[0] => 2
[1] => b
)
)
4. Use keys inside the callback
The callback only receives values. To work with keys, pass array_keys() as a second array.
<?php
$settings = ["host" => "localhost", "port" => 3306];
$lines = array_map(
fn($key, $value) => "$key: $value",
array_keys($settings),
$settings
);
echo implode("\n", $lines) . "\n";
Output:
host: localhost port: 3306
array_map() vs foreach
Both produce the same result; array_map() is an expression that returns a new array, which reads well for simple transformations. A foreach loop is better when you need to modify the original array, break out early, or perform side effects. To modify an array in place use array_walk() or foreach ($arr as &$v).
<?php
$prices = [10, 20, 30];
$doubledMap = array_map(fn($p) => $p * 2, $prices);
$doubledLoop = [];
foreach ($prices as $p) {
$doubledLoop[] = $p * 2;
}
var_dump($doubledMap === $doubledLoop);
Output:
bool(true)
Common mistakes and gotchas
- Argument order: the callback comes first in
array_map()but second inarray_filter()andarray_reduce(). - Keys and multiple arrays: string keys are kept only with a single input array.
- Outer variables: arrow functions (
fn) capture outer variables automatically; classic closures needuse ($var). - No filtering: returning
nulldoes not remove an element; combine with array_filter().