array_map is a powerful PHP function that applies a given callback function to each element of one or more arrays, and returns an array containing the results. It takes one or more arrays as input and returns a new array with the transformed elements. The original arrays remain unchanged. The syntax for array_map is as follows:
array_map(callback_function, array1 [, array2, ...])
callback_function: The callback function to be applied to each element of the input arrays.
array1: The first array to be processed by the callback function.
array2, array3, ...: (Optional) Additional arrays that can be processed simultaneously with the callback function.
By providing a callback function, you can manipulate the elements of an array and create a new array with the transformed values. It's especially useful for scenarios where you need to apply the same operation to each element in an array.
Adding multiple arrays:- In this example, we have three arrays, and the callback function takes three arguments. It adds corresponding elements from each array to create a new array with the sum of the elements.
$array1 = [1, 2, 3]; $array2 = [4, 5, 6]; $array3 = [7, 8, 9]; $result = array_map(function($a, $b, $c) { return $a + $b + $c; }, $array1, $array2, $array3); // Output: [12, 15, 18]
Squaring each element and maintaining the keys:- This example showcases how array_map works with associative arrays. It squares each value while keeping the original keys intact.
$ages = ['Alice' => 30, 'Bob' => 25, 'Charlie' => 35]; $squaredAges = array_map(function($age) { return $age * $age; }, $ages); // Output: ['Alice' => 900, 'Bob' => 625, 'Charlie' => 1225]
Concatenating two arrays element-wise:- Here, we have two arrays, $names and $surnames. The array_map() function combines them element-wise using an anonymous function to create an array with full names.
$names = ['Alice', 'Bob', 'Charlie']; $surnames = ['Smith', 'Johnson', 'Doe']; $fullNames = array_map(function($name, $surname) { return $name . ' ' . $surname; }, $names, $surnames); // Output: ['Alice Smith', 'Bob Johnson', 'Charlie Doe']
Applying a Built-in Function:- Here, we use array_map with the built-in sqrt function to calculate the square root of each element in the $numbers array.
$numbers = [2, 4, 6, 8]; $result = array_map('sqrt', $numbers); print_r($result); Array ( [0] => 1.4142135623731 [1] => 2 [2] => 2.4494897427832 [3] => 2.8284271247462 )
Using multiple arrays with null as filler for missing elements:-In this example, we use the null coalescing operator (??
) to handle arrays with different lengths. The operator returns the left operand if it exists and is not null, otherwise, it returns the right operand. This way, if an element is missing in either array, the callback function still works correctly, as null is replaced by 0 for the missing elements.
$numbers1 = [1, 2, 3]; $numbers2 = [10, 20]; $sum = array_map(function($num1, $num2) { return ($num1 ?? 0) + ($num2 ?? 0); }, $numbers1, $numbers2); print_r($sum); Array ( [0] => 11 [1] => 22 [2] => 3 )