array_map, array_walk, & array_filter: What’s the Difference (2023)

Last Updated on

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

This article reviews the array_map, array_walk, and array_filter functions and includes examples to clarify their differences. If you’re already familiar with these functions, feel free to skip to the last section for a quick overview. 

Using the PHP array_map vs array_walk & array_filter functions

PHP array_map, array_walk, and array_filter are higher-order functions. These functions take functions as arguments, commonly known as callback functions. If you’re coming from a Javascript background, you can understand the significance of the callbacks function in asynchronous programming.

array_map array_walk array_filter

However, PHP uses these callback functions on an array and consequently accesses, transforms, or filters array elements based on the logic of the callback function. Some widely known higher order functions are array_map, array_walk, and array_filter. FuelingPHP has in-depth articles on each of these. 


This article aims to analyze these functions and emphasize their differences comparatively. The article includes sections on each of these with relevant examples to give you an idea of how they work and how they differ. So, let’s cut the crap and move on to our first function: PHP array_map.

What is the PHP array_map function

PHP array_map transforms or maps array elements based on a rule. The callback function plays a vital role in defining the mapping logic. For instance, multiply numeric elements of an array with two.

PHP array_map

Let’s include an example that does what the image shows above.

<?php
 
$numbers = [ 2, 3, 4, 5 ];
 
function multiplyByTwo($num) {return $num * 2;}
 
$tranformed_numbers = array_map('multiplyByTwo', $numbers);
 
print_r($tranformed_numbers);
 
 
/*
Array
(
    [0] => 4
    [1] => 6
    [2] => 8
    [3] => 10
)
*/
?>

Voila! It takes an array of numbers, multiples them by two, and returns the transformed array. PHP 7.4.0 included support for anonymous functions mainly inspired by Javascript. With arrow functions, the syntax gets even compact.

<?php
 
$numbers = [ 2, 3, 4, 5 ];
 
$tranformed_numbers = array_map(fn($num) => $num * 2, $numbers);
 
print_r($tranformed_numbers);
 
/*
Array
(
    [0] => 4
    [1] => 6
    [2] => 8
    [3] => 10
)
*/
?>

The arrow functions are compact and could be defined within the function call, making the code much cleaner and compact.

Besides, the callback function can take more than just one argument and assumes that the callback arguments correspond to the arrays passed to the array_map functions. Here’s an example.

<?php
$roman_numerals = ["I", "II", "III", "IV", "V"];
$english_numbers = ["One", "Two", "Three", "Four", "Five"];
 
print_r(array_map(fn($rom, $eng) => "The Roman Numeral {$rom} is {$eng} in English", $roman_numerals, $english_numbers));
 
/*
OUTPUT
Array
(
    [0] => The Roman Numeral I is One in English
    [1] => The Roman Numeral II is Two in English
    [2] => The Roman Numeral III is Three in English
    [3] => The Roman Numeral IV is Four in English
    [4] => The Roman Numeral V is Five in English
)
*/
?>

What is the PHP array_walk function

PHP array_walk iterates over an array and, just like the array_map, applies a callback to array elements. However, the callback function in array_walk takes two arguments, a value and a key. So, the function is ideal for applying transforms to an associative array in PHP. Contrary to this, there is no straightforward access to array keys in the array_map.

<?php
 
//Integers array
$integers = [-1000000,-100000,-10000,-1000,-100,-10,0,10,100,1000,10000,100000,1000000];
             
 
array_walk($integers,function(&$value,$key) {
    $value *= 2;
});
 
print_r($integers);
 
/*
OUTPUT
Array
(
    [0] => -2000000
    [1] => -200000
    [2] => -20000  
    [3] => -2000  
    [4] => -200    
    [5] => -20    
    [6] => 0      
    [7] => 20      
    [8] => 200    
    [9] => 2000    
    [10] => 20000  
    [11] => 200000
    [12] => 2000000
)
*/
 
?>

Notice the & with the value argument in the callback. It implies that the value is passed by reference. Without the &, the change doesn’t reflect in the array.

What is the PHP array_filter function

PHP array_filter, as the name suggests, filters out array elements based on the callback logic. The callback returns a boolean value. So, if it returns true, the function retains the value, otherwise filtering it out. So, the count of the array in the output could be different from the count of the input array, based on the count of filtered elements.

Here’s an example of array_filter function that filters out negative numbers.

<?php
 
$numbers = [ -100, -22, -1, 0, 1, 10, 100 ];
 
$filtered_numbers = array_filter($numbers, fn($num) => $num > 0);
 
print_r($filtered_numbers);
 
/*
Array
(
    [4] => 1
    [5] => 10
    [6] => 100
)
*/
?>

Superb! Works just fine. So, by now array_filter is clear.

PHP array_map vs array_walk vs array_filter

As we have seen three of these functions already. Here’s a quick overview of these functions.

PHP array_map

This function transforms the array of elements based on the logic defined in the callback.  The callback takes one or more than one argument. Ideally, the count of arguments in the callback and the arrays should match.

PHP array_walk

This function iterates over an array and applies a callback function to elements. The callback takes two arguments: a value and a key. This function is ideal for associative arrays as it accesses key and value pairs.

PHP array_filter

This function takes an array and a callback. The callback includes a boolean condition that the function applies to the array values. Based on the outcome, it either retains or filters out the array elements.

These functions can be chained as well. Here’s an example of chaining array_map and array_filter.

<?php
 
$numbers = [ -100, -22, -1, 0, 1, 10, 100 ];
 
$filtered_numbers = array_filter(array_map(fn($num) => $num * 2,$numbers), fn($num) => $num > 0);
 
print_r($filtered_numbers);
 
/*
Array
(
    [4] => 2
    [5] => 20
    [6] => 200
)
*/
?>

The array map multiple the numbers by two and returns the transformed array. Then, the negatives are filtered out by the array_filter.

Using Common Array Functions like array_map & array_walk

This article includes an overview with examples of PHP array_map, array_walk, and array_filter functions. These higher order functions are popular but they can be confusing. So, the article explains the differences between these three as well. Hopefully, you’re now familiar with what higher-order functions are and specifically these three functions.

FuelingPHP has an extensive collection of intuitive and in-depth articles and tutorials about PHP. Check these out for learning more about PHP.

PHP Fundamentals Article Series

This article is part of our series on learning the fundamentals of PHP web development. If you are stepping into your PHP journey. Check out the articles in this series to get deeper into PHP.

Did you find this article helpful?

Join the best weekly newsletter where I deliver content on building better web applications. I curate the best tips, strategies, news & resources to help you develop highly-scalable and results-driven applications.

Build Better Web Apps

I hope you're enjoying this article.

Get the best content on building better web apps delivered to you.