DEV Community

Cover image for PHP array_map Function: Detailed Guide
Michael Para
Michael Para

Posted on

PHP array_map Function: Detailed Guide

The PHP array_map function applies a callback to each element of one or more arrays.

Syntax of PHP array_map Function

PHP array_map function returns a new array containing the results. This function helps process array elements without using loops.

array_map(callback, array1, array2, ...)
Enter fullscreen mode Exit fullscreen mode
  • callback: A function name or an anonymous function. It takes the array element(s) as argument(s).
  • array1, array2, ...: One or more arrays to process.

The function calls the callback on every element from the input arrays. It passes corresponding elements from each array to the callback. Then it collects the callback return values into a new array.

If arrays have different lengths, the function stops at the shortest array's length.

For example:

$numbers = [1, 2, 3, 4];
$squares = array_map(function($n) {
    return $n * $n;
}, $numbers);

print_r($squares);

Enter fullscreen mode Exit fullscreen mode

Output:

Array
(
    [0] => 1
    [1] => 4
    [2] => 9
    [3] => 16
)
Enter fullscreen mode Exit fullscreen mode

This example squares every number in the input array.

Using Multiple Arrays

You can provide multiple arrays to array_map. The callback receives one argument per array, each representing the corresponding element.

$array1 = [1, 2, 3];
$array2 = [4, 5, 6];

$sum = array_map(function($a, $b) {
    return $a + $b;
}, $array1, $array2);

print_r($sum);
Enter fullscreen mode Exit fullscreen mode

Output:

Array
(
    [0] => 5
    [1] => 7
    [2] => 9
)
Enter fullscreen mode Exit fullscreen mode

The function adds corresponding elements from both arrays.

Using Named Callback Functions

Instead of PHP anonymous functions, you can use existing function names as callbacks.

function double($n) {
    return $n * 2;
}

$numbers = [1, 2, 3];
$doubled = array_map('double', $numbers);

print_r($doubled);
Enter fullscreen mode Exit fullscreen mode

Here is output:

Array
(
    [0] => 2
    [1] => 4
    [2] => 6
)
Enter fullscreen mode Exit fullscreen mode

Using Built-in PHP Functions as Callback

You can use built-in PHP functions with array_map.

$strings = ['apple', 'banana', 'cherry'];
$upper = array_map('strtoupper', $strings);

print_r($upper);
Enter fullscreen mode Exit fullscreen mode

Output:

Array
(
    [0] => APPLE
    [1] => BANANA
    [2] => CHERRY
)
Enter fullscreen mode Exit fullscreen mode

array_map returns an array with the mapped results. It does not modify the original arrays.

Here are important notes:

  • The original arrays stay unchanged.
  • The returned array keys match the input arrays' keys.
  • If callback is null, array_map returns the arrays merged into one multidimensional array.
  • The function stops processing at the shortest input array.

Examples

$array1 = [1, 2, 3];
$array2 = ['a', 'b', 'c'];

$result = array_map(null, $array1, $array2);
print_r($result);
Enter fullscreen mode Exit fullscreen mode

Output:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => a
        )

    [1] => Array
        (
            [0] => 2
            [1] => b
        )

    [2] => Array
        (
            [0] => 3
            [1] => c
        )
)
Enter fullscreen mode Exit fullscreen mode
  • The function groups elements with the same keys from each array.
  • Transform all elements in an array.
  • Combine multiple arrays element-wise.
  • Format or sanitize input data.
  • Change array values using a function.

Summary

The array_map function lets you transform arrays with a callback function. It works well for simple element-wise operations without explicit loops. It handles one or more arrays and returns a new array with mapped results. Keep original arrays safe and keys preserved.

Top comments (0)