Difference between array_map, array_walk and array_filter

Andrei Birta
3 min readJan 14, 2023

--

array_map(), array_walk() and array_filter() all of them are functions that allow you to perform operations on arrays. They are similar in that they all operate on arrays, but they have different purposes and behave differently.

array_map()

Applies a callback function to each element of an array and returns a new array containing the modified elements. The callback function can be a built-in function, or a user-defined function.

Below is an example of how array_map() can be used:

$array1 = [2, 4, 6, 8, 10];
$doubled = array_map('double', $array1);
// $doubled is now [4, 8, 12, 16, 20]

function double($y) {
return $y * 2;
}

In this example, the double() function is applied to for each element of the $array1 and a new array $doubled is created containing the doubled values.

array_map() can also be used with multiple arrays. In this case, the callback function should take the same arguments as there are arrays. The callback function is applied to the elements of each array in parallel, meaning that the first element of the first array is passed to the callback function with the first element of the second array and so on.

$array1 = [1, 2, 3];
$array2 = [10, 20, 30];
$sum = array_map(function($x, $y) { return $x + $y; }, $array1, $array2);
// $sum is now [11, 22, 33]

In the example from above the anonymous function is applied to the elements from the $array1 and $array2 in parallel and the array $sum is created containing the sums of the corresponding elements.

array_walk()

Applies a callback function to each element of an array. However, unlike array_map(), it modifies the array itself rather than returning a new array.

Here is an example of how it works:

$array1 = [2, 4, 6, 8, 10];
array_walk($array1, 'double');
// $array1 is now [4, 8, 12, 16, 20]

function double(&$x) {
$x *= 2;
}

In this example, the double() function is applied to each element of the $array1 doubling the value of each element. Note that the callback function must accept a reference to the array element, indicated by the & symbol in the function definition.

array_filter()
Is a function in PHP that filters the elements of an array using a callback function. It returns a new array containing only the elements for which the callback function returns true.

The array_filter() can be used like in the example from below:

$array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$evens = array_filter($array1, 'is_even');
// $evens is now [2, 4, 6, 8, 10]

function is_even($x) {
return $x % 2 == 0;
}

In this example, the is_even() function is used as a callback for array_filter(). This function checks if a given number is even by checking if it is divisible by 2. array_filter() applies this function to each element of the $array1 and returns a new array containing only the even elements.

In general, array_filter() tends to be the fastest of these three functions. This is because array_filter() simply loops through the array and applies a callback function to each element, returning only the elements for which the callback function returns true. It does not modify the elements or create a new array, so it has less overhead than array_map() and array_filterwhich both creates new arrays or modify the original array.

However, the actual performance of these functions may depend on a variety of factors, including the size of the array, the complexity of the callback function and the hardware or software environment in which the functions are running. Therefore, it is always a good idea to test the performance of your code in your specific use case.

In conclusion, is important to note that the performance of these functions may not always be a critical factor in your application. In many cases, the simplicity and clarity of the code may be more important than the absolute performance. It is generally a good idea to choose the function that is most appropriate for your needs, rather than simply trying to optimize for raw performance without considering other factors.

Get more valuable insights and tips by following me!
Also check my article about the TDD process, eager loading in Laravel or a list of articles about the PHP or different type of architecture.

--

--