PHP Array Map Function: A Comprehensive Guide
The array_map function in PHP is an essential tool for transforming arrays of data. It allows developers to apply a callback function to each element of an
The array_map function in PHP is an essential tool for transforming arrays of data. It allows developers to apply a callback function to each element of an array, and then return a new array with the results. This is a versatile and powerful technique for processing data in PHP, and can be used for a wide range of applications.
How does array_map work?
The basic syntax of the array_map function is as follows:
PHP array_map function syntax
array_map(callback, array1, array2, ...);Here, the callback argument is the function that will be applied to each element of the arrays. The other arguments are the arrays to be processed. For example, consider the following code:
PHP example array_map function usage
This code defines a simple function, square, that takes a number and returns its square. The array_map function is then used to apply this function to each element of the $numbers array. The result is a new array, $squared, that contains the squares of the original numbers.
Note on array lengths: When multiple arrays are passed to array_map, it continues processing until the longest array is exhausted. If arrays have unequal lengths, missing elements in shorter arrays are automatically treated as null.
Note on modern syntax: In PHP 7.4+, you can use arrow functions for concise callbacks. For example: array_map(fn($n) => $n * $n, $numbers);
Real-world examples of array_map
The array_map function is extremely versatile, and can be used in many different contexts. Here are a few examples to give you a sense of its power:
Transforming arrays of strings
PHP Transforming arrays of strings by using array_map function
In this example, the array_map function is used to convert an array of strings to uppercase. The upper function takes a string and returns its uppercase equivalent. When this function is applied to each element of the $words array, the result is a new array with the uppercase versions of the original words.
Combining arrays with array_map
PHP Combining arrays with array_map
In this example, two arrays, $a and $b, are combined into a third array, $c. The sum function takes two arguments and returns their sum. The array_map function is used to apply this function to each corresponding pair of elements in $a and $b. The result is a new array, $c, that contains the sums of the original pairs.
Zipping arrays with a null callback
If you pass null as the callback, array_map zips the arrays together — each result element is an array of the corresponding values from every input array. This is handy for pairing related data without writing a function.
PHP zipping arrays with array_map and a null callback
<?php
$ids = [1, 2, 3];
$names = ['Alice', 'Bob', 'Carol'];
$pairs = array_map(null, $ids, $names);
print_r($pairs);
?>The result is [[1, 'Alice'], [2, 'Bob'], [3, 'Carol']] — one inner array per row.
Gotchas and things to watch out for
- Keys are preserved only with a single array. When you pass one array,
array_mapkeeps the original keys. With two or more arrays it re-indexes the result with sequential integer keys, so any string keys are lost. array_mapdoes not modify the original array. It always returns a new array. If you want to transform an array in place, usearray_walkinstead.- The callback must be callable. Pass a string function name (
'square'), an anonymous function, an arrow function, or an[$object, 'method']array. See PHP callback functions for the full list of accepted forms.
When to use array_map vs. other array functions
- Use
array_mapto transform every element into a new value (one input element → one output element). - Use
array_filterwhen you want to keep only the elements that pass a test. - Use
array_reducewhen you want to collapse the array into a single value, such as a sum or a concatenated string.
Conclusion
The array_map function in PHP is an incredibly powerful tool for transforming arrays of data. Whether you are converting strings to uppercase, combining arrays, or performing some other type of calculation, array_map is the perfect tool for the job. With its flexible syntax and ability to work with multiple arrays at once, it is a must-know function for any PHP developer.