PHP Function Reference

PHP array_map() Function



The PHP array_map() function applies the callback to the elements of the given arrays. It returns an array containing the results of applying the callback to the corresponding index of array (and arrays if more arrays are provided) used as arguments for the callback. The number of parameters that the callback function accepts should match the number of arrays passed to this function. Excess input arrays are ignored.

Syntax

array_map(callback, array, arrays)

Parameters

callback

Required. Specify a function to run for each element in each array.

null is provided as callback it performs a zip operation on the given arrays. If only array is provided, it returns the input array only.
array Required. Specify an array to run through the callback function.
arrays Optional. Specify more arrays to run through the callback function.

Return Value

Returns an array containing the results of applying the callback function to the corresponding index of array (and arrays if more arrays are provided) used as arguments for the callback.

The returned array will preserve the keys of the array argument if and only if exactly one array is passed. If more than one array is passed, the returned array will have sequential integer keys.

Exceptions

An ArgumentCountError is thrown if an insufficient number of arguments is provided.

Example: array_map() example

The example below shows the usage of array_map() function.

<?php
function square($n) {
  return ($n * $n);
}

$arr = [1, 2, 3, 4, 5];

//applying the square function
//to each element of $arr
$result = array_map('square', $arr);

//displaying the result
print_r($result);
?>

The output of the above code will be:

Array
(
    [0] => 1
    [1] => 4
    [2] => 9
    [3] => 16
    [4] => 25
)

Example: array_map() - using more arrays

When using two or more arrays, usually they should be of equal length because the callback function is applied in parallel to the corresponding elements. If the arrays are of unequal length, shorter ones will be extended with empty elements to match the length of the longest.

Consider the example below where multiple arrays are used to run through the callback function.

<?php
function sum($x, $y) {
  return ($x + $y);
}

$arr1 = [1, 2, 3, 4, 5];
$arr2 = [10, 20, 30, 40, 50];

//applying the sum function to each 
//elements of $arr1 and $arr2
$result = array_map('sum', $arr1, $arr2);

//displaying the result
print_r($result);
?>

The output of the above code will be:

Array
(
    [0] => 11
    [1] => 22
    [2] => 33
    [3] => 44
    [4] => 55
)

Example: array_map() using a lambda function

Consider the example below where a lambda function is used as callback function.

<?php
$arr = [1, 2, 3, 4, 5];

//applying the lambda function
//to each element of $arr
$result = array_map(fn($x): int => $x * 5, $arr);

//displaying the result
print_r($result);
?>

The output of the above code will be:

Array
(
    [0] => 5
    [1] => 10
    [2] => 15
    [3] => 20
    [4] => 25
)

Example: performing a zip operation of arrays

The zip operation can be performed on arrays by passing null as callback function.

<?php
$arr1 = [1, 2, 3];
$arr2 = ['a', 'b', 'c'];
$arr3 = ['one', 'two', 'three'];

//performing zip operation on arrays
$result = array_map(null, $arr1, $arr2, $arr3);

//displaying the result
print_r($result);
?>

The output of the above code will be:

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

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

    [2] => Array
        (
            [0] => 3
            [1] => c
            [2] => three
        )

)

Example: array_map() - associative arrays

This function does not directly support using the array key as an input, which can be achieved using array_keys().

<?php
function desc($k, $v) {
  return "$k is mapped to $v";
}

$arr = array('red' => 10,
             'green' => 20,
             'blue' => 30);

//applying desc function to $arr
$result = array_map('desc', array_keys($arr), $arr);

//displaying the result
print_r($result);
?>

The output of the above code will be:

Array
(
    [0] => red is mapped to 10
    [1] => green is mapped to 20
    [2] => blue is mapped to 30
)

❮ PHP Array Reference