Difference between array_map, array_walk and array_filter

Difference between array_map, array_walk and array_filter

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Difference between array_map, array_walk and array_filter

PHP provides a suite of iterator functions designed to manipulate arrays without explicit foreach loops, but they serve distinct architectural purposes. The confusion often arises because all three accept callbacks and iterate over elements, yet their return types, mutation behaviors, and use cases differ fundamentally. Understanding these distinctions prevents logic errors where you might expect an array to be returned when a procedural action was actually required.

array_map: Transforming Data into a New Array

The primary contract of array_map is transformation. It applies a callback function to every element in an input array and must return a new array containing the results. If your goal is to derive new data from existing data, this is the tool.

Consider a scenario where you have a list of raw strings representing prices and you need to convert them into formatted currency objects or simply cast them to floats. The function collects the output of your callback for every single element and stitches it back together into a fresh array structure.

$rawPrices = ['10', '20', '30'];

$formatter = function($price) {
    return floatval($price);
};

$formattedPrices = array_map($formatter, $rawPrices);

// Result: [10.0, 20.0, 30.0]

A critical detail often overlooked is that array_map will throw a warning if the callback encounters an element it cannot process and doesn't return a value, or if the input array contains non-arrayable values depending on PHP version constraints. It also accepts multiple arrays as arguments, applying the callback to corresponding elements of each array simultaneously.

array_walk: Procedural Side Effects

In contrast, array_walk is designed for side effects. It applies a callback to every element in an array but does not return a modified or new array. The function executes the logic and discards the result, effectively making it a procedural action rather than a functional transformation.

You would use array_walk when you need to perform an I/O operation, log data, or modify global state based on the array contents, without caring about the resulting structure of the original array. However, be cautious: standard array_walk passes the element by reference only if the callback accepts a second argument (the key) or modifies the value passed via the first argument directly in specific contexts, but it does not return the modified values for you to capture in a new variable directly from the function call.

$items = ['apple', 'banana', 'cherry'];

// This logs each item but $items remains unchanged as a returned value
array_walk($items, function($item) {
    error_log("Processing: " . $item);
});

If you attempt to use array_map for logging, you are forced to ignore its return value or deal with the fact that it tries to return something even if you don't want to. array_walk explicitly signals that the operation is about doing something to the data in place rather than producing a new dataset.

array_filter: Selection Logic

The third function, array_filter, operates on selection logic. Its callback accepts an element and returns a boolean. If the callback returns true (or a truthy value), the element is kept in the resulting array; if it returns false, the element is excluded. The return type is always a new array containing only the filtered elements.

$numbers = [1, 2, 3, 4, 5];

$evenNumbers = array_filter($numbers, function($num) {
    return $num % 2 === 0;
});

// Result: [2, 4]

A common pitfall with array_filter is the default behavior regarding false values. By default, if your callback returns false, the element is removed. However, you can change this behavior using the $flags parameter to include elements where the callback returned false. This distinction is vital when cleaning up data sets based on complex validation rules.

Interchangeability and Trade-offs

Can these functions be used interchangeably? Generally, no. Attempting to swap them leads to broken code or silent logic failures. You cannot use array_walk if you need the transformed array, and you cannot use array_map if you only want to trigger a side effect like writing to a file while ignoring the result.

While modern PHP syntax allows for concise iteration with foreach, these iterator functions remain valuable in specific contexts:

  1. Functional Composition: Chaining operations where the output of one becomes the input of another without intermediate variables.
  2. Legacy Codebases: Refactoring older code that relied on procedural patterns without introducing new dependencies.
  3. Laravel Collections: While Laravel's collect() method offers similar functional methods (map, walk, filter), understanding the underlying PHP functions helps when debugging native array operations or interacting with raw arrays in controllers and services.

Choosing the right tool depends on whether you are transforming data, filtering it, or merely performing an action on it. Misidentifying the purpose of these functions is a frequent source of bugs where developers expect a return value from array_walk or lose filtered elements due to misunderstanding array_filter's boolean expectations.

Our network