The array_map() function in PHP lets you apply a callback function to every item in an array and returns a new array with the results. It’s useful when you want to transform values in a clean and readable way—That does not need you to write a foreach loop.
PHP array_map Syntax
Here is the array_map in PHP syntax:
array_map(callable $callback, array $array1, array ...$arrays): array
- $callback: A function that defines how each element should change.
- $array1: The array to transform.
- $arrays: (Optional) One or more arrays to combine with the first.
Here is an example:
$prices = [100, 250, 75];
$withCurrency = array_map(function($price) {
return '$' . number_format($price, 2);
}, $prices);
print_r($withCurrency);
Its output:
Array
(
[0] => $100.00
[1] => $250.00
[2] => $75.00
)
We take an array of prices and format each with a dollar sign and two decimal places. array_map() applies the same formatting function to all.
Example 1: Replace Vowels in Words
$words = ['apple', 'orange', 'grape'];
$replaced = array_map(function($word) {
return str_replace(['a', 'e', 'i', 'o', 'u'], '*', $word);
}, $words);
print_r($replaced);
Output:
Array
(
[0] => *ppl*
[1] => *r*ng*
[2] => gr*p*
)
We transform each word and replace vowels with *. This type of transformation helps mask or filter text.
Example 2: Merge Names and Titles with Multiple Arrays
$names = ['John', 'Amina', 'Carlos'];
$titles = ['Mr.', 'Dr.', 'Prof.'];
$fullNames = array_map(function($title, $name) {
return "$title $name";
}, $titles, $names);
print_r($fullNames);
Here is the output:
Array
(
[0] => Mr. John
[1] => Dr. Amina
[2] => Prof. Carlos
)
We pass two arrays—one with titles, the other with names. The function joins them into full names.
Example 3: Use Anonymous Function to Format Inventory Items
Here is an example with The anonymous function in PHP:
$inventory = ['Pen', 'Notebook', 'Stapler'];
$formatted = array_map(function($item) {
return "Item: " . strtoupper($item);
}, $inventory);
print_r($formatted);
The output:
Array
(
[0] => Item: PEN
[1] => Item: NOTEBOOK
[2] => Item: STAPLER
)
This example uses an anonymous function (also called a closure) directly inside array_map() to transform each item. We convert the item name to uppercase and add a label. This is useful when you format output and don’t need to reuse the function elsewhere.
Conclusion
The array_map() function in PHP is a simple yet powerful way to transform array values with a callback. Instead of using repetitive loops, you apply a clean function to each element and return a new, updated array.
In this guide, we explored:
- How array_map() works with a single array.
- How to combine multiple arrays.
- How to use the anonymous function in PHP.
mous functions for inline logic.
Now, you write more readable and efficient PHP code—especially when you work with structured or repetitive data.
Use it when:
- You want to apply the same change to every item.
- You process arrays in a functional style.
- You want to keep your code short, expressive, and loop-free.
Top comments (0)