Content · Programming · PHP

PHP array_walk_recursive() Function: Usage and Examples

Understand array_walk_recursive() in PHP with syntax, working code examples, expected output, practical use cases, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When writing PHP applications, you may need to apply a callback to values nested inside arrays. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.

The array_walk_recursive() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.

What Is array_walk_recursive() in PHP?

Apply a callback to values nested inside arrays.

Its syntax is:

array_walk_recursive(array|object &$array, callable $callback, mixed $arg = null): true

Parameters used by this operation:

  • &$array — The collection being examined or updated.
  • $callback — A callable that controls the comparison or transformation; callbacks should follow the expected return contract.
  • $arg — The value supplied for this argument affects the operation.

The callback receives leaf values, not arrays as array values. You cannot modify array structure reliably within the callback.

Example 1: Basic Usage

Start with a small, self-contained input so you can inspect exactly how the operation behaves.

<?php

$payload = ["user" => ["name" => " Maya ", "city" => " Paris "]];
array_walk_recursive($payload, function (&$value): void { $value = trim($value); });
print_r($payload);

Output:

Array
(
    [user] => Array
        (
            [name] => Maya
            [city] => Paris
        )

)

The output shows the result of the first call. Check the printed keys and values as well as their order.

Example 2: Another Important Case

This variation highlights another input shape, a comparison option, or a change in the collection.

<?php

$measurements = ["group" => [2, 3], "single" => 4];
array_walk_recursive($measurements, function (&$value): void { $value *= 10; });
print_r($measurements);

Output:

Array
(
    [group] => Array
        (
            [0] => 20
            [1] => 30
        )

    [single] => 40
)

Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.

Practical Example: Clean string leaves in a nested configuration structure

Clean string leaves in a nested configuration structure. Save the code in a PHP file and adapt the sample data to your project.

<?php

$settings = ["interface" => ["name" => " Portal ", "theme" => " Dark "], "owner" => " Maya "];
array_walk_recursive($settings, function (&$value): void { if (is_string($value)) { $value = trim($value); } });
print_r($settings);

Output:

Array
(
    [interface] => Array
        (
            [name] => Portal
            [theme] => Dark
        )

    [owner] => Maya
)

The output comes from the completed workflow shown above. When adapting it, change the source records and keep the same checks for missing, unexpected, or invalid values.

Important Behavior and Edge Cases

The callback receives leaf values, not arrays as array values. You cannot modify array structure reliably within the callback.

  • Test the operation with empty or missing input and decide how your application should respond.
  • Read the return value independently from any changes to variables passed by reference.
  • Validate untrusted input before using the result for permissions, database changes, or user-visible output.

Comparison With Related Functions

Function or ApproachWhen to Use It
array_walk_recursive()Apply a callback to values nested inside arrays
Related functionsarray_walk() visits only top-level entries; RecursiveArrayIterator offers traversal control.

Common Beginner Mistakes

MistakeCorrect Approach
Forgetting the reference on a callback valueUse a reference parameter when the intention is to change the original array element.
Replacing array structure inside a callbackUse an explicit loop if you need to add or remove keys.
Confusing shallow and recursive traversalThe callback receives leaf values, not arrays as array values. You cannot modify array structure reliably within the callback.

Key Takeaways

  • array_walk_recursive() helps you apply a callback to values nested inside arrays.
  • The callback receives leaf values, not arrays as array values. You cannot modify array structure reliably within the callback.
  • array_walk() visits only top-level entries; RecursiveArrayIterator offers traversal control.
  • Use the practical example as a starting point, but validate real application inputs.

array_walk_recursive() is useful when you need to apply a callback to values nested inside arrays. Use its documented rules and the examples above to choose the right behavior for your data.

Share this resourceSend this page to someone who may find it useful.