Zipping up multiple arrays using array_map in PHP
Little-known features are the best when you find them unknowingly. One such feature I came across recently is of the PHP’s popular array_map function.
So, the array_map function is usually used for applying the callback to the elements of the given arrays but did you know you can also zip up multiple arrays of the same length using
By the way, zipping up is an operation where you can build an array of arrays. Let’s look at how you can do this with
Zipping up arrays
For instance, let’s say, we have three arrays of the same length with the following values.
$users = ['Amit', 'Jemini', 'Cherika']; $age = [31, 30, 4]; $sex = ['Male', 'Female', 'Female'];
Now, a zipping operation can create a resulting array from these arrays which look like so.
Array (  => Array (  => 'Amit'  => 31  => 'Male' )  => Array (  => 'Jemini'  => 30  => 'Female' )  => Array (  => 'Cherika'  => 4  => 'Female' ) )
How would you do this using
array_map for zipping
You can use
array_map to easily perform zipping operation by using
null as the name of the callback function in the first argument and passing all the available arrays as the rest of the arguments like so.
$users = ['Amit', 'Jemini', 'Cherika']; $age = [31, 30, 4]; $sex = ['Male', 'Female', 'Female']; $userDetails = array_map(null, $users, $age, $sex); print_r($userDetails);
This will return the zipped up array of arrays just like how I’ve mentioned in the previous section.
It’s important to note here is when using two or more arrays, they should be of equal length because the callback function (in this case
null) 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.