CodeNewbie Community 🌱

Montasser Mossallem
Montasser Mossallem

Posted on

PHP array_map() Function: The Complete Guide

Image description
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
Enter fullscreen mode Exit fullscreen mode
  • $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);
Enter fullscreen mode Exit fullscreen mode

Its output:

Array
(
    [0] => $100.00
    [1] => $250.00
    [2] => $75.00
)
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output:

Array
(
    [0] => *ppl*
    [1] => *r*ng*
    [2] => gr*p*
)
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Here is the output:

Array
(
    [0] => Mr. John
    [1] => Dr. Amina
    [2] => Prof. Carlos
)

Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

The output:

Array
(
    [0] => Item: PEN
    [1] => Item: NOTEBOOK
    [2] => Item: STAPLER
)

Enter fullscreen mode Exit fullscreen mode

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:

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 (1)

Collapse
 
keithwalker profile image
Keithwalker

The array_map() function in PHP is a great tool for transforming values in an array without writing loops. You just pass a function and the array—it handles the rest. For example, if you want to format prices, clean text, or combine two arrays (like names and titles), array_map() makes it super easy and clean.

It's especially helpful when you're working with structured data or want more readable code. If you're diving deeper into PHP and looking for efficient tools, exploring popular PHP frameworks can really boost your productivity and code quality.