array_map()
Last updated onThe PHP array_map takes an array and, for each element in that array, lets you apply a callback function to transform or manipulate each item however you'd like. You need at least two parameters: the first one is for the function—aka callback—and the second is for the array you are working on.
And if you want to use more than one array, well,
can handle that too. array_map
PHP array_map Syntax
Here’s the basic setup:
array_map(callback, array1, ...) // Additional arrays can be included
: The function to apply to all elements.callback
: The array you want to manipulate.array1
If you’re using multiple arrays, make sure the function can handle that many arguments. For example, if you have five arrays, your function will need to handle five parameters. Here’s a quick look:
<?php
function callback($param1, $param2, $param3) {
// Your code here
}
array_map('callback', $array1, $array2, $array3);
Let’s move forward to see examples.
Transforming Arrays with Custom Functions
Let's start with something simple, like taking an array of numbers and wanting to double each one.
makes this a breeze:array_map
$array = [1, 2, 3, 4, 5];
$doubleArray = array_map(function($value) {
return $value * 2;
}, $array);
// Result: $doubleArray = [2, 4, 6, 8, 10]
This takes each number in
, doubles it, and returns a new array with the updated values.$array
Applying Custom Functions with array_map
Moving on from basic math, you can use
to perform more complex tasks too. Say we want to square each number:array_map
function square($value) {
return $value ** 2;
}
$array = [2, 4, 6, 8, 10];
$squaredArray = array_map('square', $array);
// Result: $squaredArray = [4, 16, 36, 64, 100]
The
function does the squaring here, while square
does the work of applying it across the entire array.array_map
Synchronizing Multiple Arrays
One of the great things about
is that it lets you use more than one array at a time. Here's an example where we add corresponding elements in two arrays:array_map
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
$sumArray = array_map(function($a, $b) {
return $a + $b;
}, $array1, $array2);
// Result: $sumArray = [5, 7, 9];
In the example above,
takes one element from each array and puts all of them together to create a new array containing the sums.array_map
String Handling with array_map
isn't just for numbers—you can use it to operate on strings too. Here's how you might strip out extra spaces from each string in an array:array_map
$array = [' apple ', 'banana ', ' cherry'];
$trimmedArray = array_map('trim', $array);
// Result: $trimmedArray = ['apple', 'banana', 'cherry']
Here,
removes the spaces, and trim
applies it to each element in the array.array_map
Handling Associative Arrays
You can also use
on associative arrays. Suppose we have the following array of students and want to change each student's grade to uppercase:array_map
$students = [
['name' => 'John', 'grade' => 'a'],
['name' => 'Jane', 'grade' => 'b'],
['name' => 'Doe', 'grade' => 'c']
];
$uppercaseGrades = array_map(function($student) {
$student['grade'] = strtoupper($student['grade']);
return $student;
}, $students);
// Result: $uppercasedGrades has grades in uppercase
This callback function modifies the grade
key for each student without changing their name.
Working with Arrays of Different Lengths
If you use
with arrays that are of uneven length, it will stop as soon as it reaches the end of the shortest array. Here's what that looks like:array_map
$array1 = [1, 2, 3];
$array2 = [4, 5];
$resultArray = array_map(function($a, $b) {
return $a + $b;
}, $array1, $array2);
// Result: $resultArray = [5, 7, 3]
In this case,
processes the matching elements; if one of the arrays reaches its end, it continues with the remaining values of the other array.array_map
Using array_map Inside a Class
You may want to consider using
with a class for better organization; here’s how that is done:array_map
class Transformer {
public function doubleValue($value) {
return $value * 2;
}
}
$array = [1, 2, 3, 4, 5];
$transformer = new Transformer();
$transformedArray = array_map([$transformer, 'doubleValue'], $array);
// Result: $transformedArray = [2, 4, 6, 8, 10]
Here, the
class has a method to double values. We instantiate the class and then apply Transformer
with the method to every item in the array.array_map
Wrapping Up
One of the powerful ways to handle arrays in PHP is mapping. It keeps your code clean, saves time, and opens up a world of possibilities—from simple transformations to complex operations across multiple arrays.
As you go through the examples above, you’ll see how useful
can be when trying to write efficient PHP.array_map
Frequently Asked Questions (FAQs)
What is the array_map function in PHP, and how does it work?
Can array_map handle multiple arrays at once?
How can I use a custom function with array_map?
Does array_map work with associative arrays?
What happens if I use array_map with arrays of different lengths?
Can I use array_map with a class method?
What’s the difference between array_map and array_walk?
Did you find this tutorial useful?
Your feedback helps us improve our tutorials.