Table of contents
1.
Introduction
2.
Definition and Usage  
3.
Syntax
4.
Parameters
5.
Return Value
6.
Example 1: Basic Usage  
7.
Example 2: Using array_map() with a Simple Function
8.
Example 3: Using Anonymous Functions with array_map()
9.
Example 4: Using Anonymous Functions  
10.
Example 5: Working with Multiple Arrays  
11.
Example 6: Applying array_map() on Multiple Arrays
12.
Creating an Array of Arrays Using array_map()
13.
Frequently Asked Questions
13.1.
What happens if the arrays passed to array_map() have different lengths?
13.2.
Can we use built-in PHP functions with array_map()?
13.3.
What is the difference between array_map() and array_walk()?
14.
Conclusion
Last Updated: Feb 14, 2025
Easy

PHP array_map() Function

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The PHP array_map() function is used to apply a callback function to each element of an array and return a new modified array. It is commonly used for transforming array values without using loops, making code cleaner and more efficient. This function is useful for tasks like data formatting and manipulation.

PHP array_map() Function

In this article, we will discuss the syntax, parameters, return value, and how to create an array of arrays using array_map(). 

Definition and Usage  

The `array_map` function in PHP is used to apply a callback function to each element of an array. It takes at least two arguments: the callback function & one or more arrays. The callback function defines the operation you want to perform on each element, & `array_map` returns a new array containing the modified elements.  

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

  1. callback (required): A function that will be applied to each element of the array.
     
  2. array1 (required): The main array whose elements will be processed.
     
  3. ...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.

Live masterclass