Example 1 — Double Every Number
Apply a small arrow function to each item.
<?php
$numbers = [1, 2, 3];
$doubled = array_map(fn($n) => $n * 2, $numbers);
echo implode(", ", $doubled), PHP_EOL; How It Works
- Create an array. It holds the numbers 1, 2 and 3.
- Write the callback.
fn($n) => $n * 2returns twice its input. - Call array_map(). PHP applies the callback to each item in order.
- Print the new array. The original
$numbersis unchanged.

