Syntax
array array_map(callable $callback, array $array1, array ...$arrays)
Explanation:
- $callback: A function that is applied to each element of the array.
- $array1: The first array on which the function will be applied.
- $arrays (optional): Additional arrays can be passed to apply the function to multiple arrays at once.
Parameters
- callback (required): A function that will be applied to each element of the array.
- array1 (required): The main array whose elements will be processed.
- ...arrays (optional): Additional arrays can be passed for element-wise processing.
Return Value
The array_map() function returns a new array where each element is the result of applying the callback function to the corresponding element(s) of the input array(s).
Example 1: Basic Usage
Let’s say you have an array of numbers, & you want to square each number. Let’s see how you can do it using `array_map`:
<?php
// Define a callback function to square a number
function square($number) {
return $number $number;
}
// Original array
$numbers = [1, 2, 3, 4, 5];
// Apply the callback function to each element
$squaredNumbers = array_map('square', $numbers);
// Print the result
print_r($squaredNumbers);
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[0] => 1
[1] => 4
[2] => 9
[3] => 16
[4] => 25
)
In this example, the `square` function is applied to each element of the `$numbers` array, & the result is a new array with squared values.
Example 2: Using array_map() with a Simple Function
Let's see a basic example where we use array_map() to square each element in an array:
function square($num) {
return $num * $num;
}
$numbers = [1, 2, 3, 4, 5];
$squaredNumbers = array_map("square", $numbers);
print_r($squaredNumbers);

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[0] => 1
[1] => 4
[2] => 9
[3] => 16
[4] => 25
)
Example 3: Using Anonymous Functions with array_map()
Instead of defining a separate function, we can use an anonymous function:
$numbers = [10, 20, 30, 40];
$squaredNumbers = array_map(function($num) {
return $num * $num;
}, $numbers);
print_r($squaredNumbers);

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[0] => 100
[1] => 400
[2] => 900
[3] => 1600
)
Example 4: Using Anonymous Functions
You can also use anonymous functions (closures) with `array_map` for more concise code. Let’s discuss the same example rewritten using an anonymous function:
<?php
// Original array
$numbers = [1, 2, 3, 4, 5];
// Apply an anonymous function to square each number
$squaredNumbers = array_map(function($number) {
return $number $number;
}, $numbers);
// Print the result
print_r($squaredNumbers);
?>
```

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[0] => 1
[1] => 4
[2] => 9
[3] => 16
[4] => 25
)
This approach is useful when you don’t want to define a separate function for a one-time operation.
Example 5: Working with Multiple Arrays
`array_map` can also handle multiple arrays. The callback function will receive elements from each array as arguments. Let’s take an example where we add corresponding elements from two arrays:
<?php
// Define a callback function to add two numbers
function add($a, $b) {
return $a + $b;
}
// Original arrays
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
// Apply the callback function to corresponding elements
$result = array_map('add', $array1, $array2);
// Print the result
print_r($result);
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[0] => 5
[1] => 7
[2] => 9
)

You can also try this code with Online PHP Compiler
Run Code
In this case, the `add` function takes two arguments, & `array_map` applies it to pairs of elements from `$array1` & `$array2`.
Example 6: Applying array_map() on Multiple Arrays
If multiple arrays are provided, array_map() applies the callback function element-wise:
function add($a, $b) {
return $a + $b;
}
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
$sumArray = array_map("add", $array1, $array2);
print_r($sumArray);

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[0] => 5
[1] => 7
[2] => 9
)
Creating an Array of Arrays Using array_map()
We can use array_map() to structure data into an array of arrays. Let’s see an example:
$names = ["Alice", "Bob", "Charlie"];
$ages = [25, 30, 22];
$combinedArray = array_map(null, $names, $ages);
print_r($combinedArray);

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[0] => Array ( [0] => Alice [1] => 25 )
[1] => Array ( [0] => Bob [1] => 30 )
[2] => Array ( [0] => Charlie [1] => 22 )
)
Here, null as a callback function groups corresponding elements from both arrays into a new array.
Frequently Asked Questions
What happens if the arrays passed to array_map() have different lengths?
If the arrays have different lengths, array_map() stops processing when the shortest array ends.
Can we use built-in PHP functions with array_map()?
Yes, you can pass PHP built-in functions like strtoupper or abs directly as a callback.
What is the difference between array_map() and array_walk()?
array_map() returns a new array, while array_walk() modifies the original array in place.
Conclusion
In this article, we learned about the PHP array_map() function, how it works, and how to use it to modify array elements efficiently. This function helps apply a callback to each array value, making code cleaner and more readable. Understanding array_map() allows developers to write optimized and maintainable PHP programs with less effort.