PHP array_walk_recursive() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_walk_recursive() runs a function on every item of an array, including items inside nested arrays.

It works like array_walk(), but whenever it finds an inner array it goes inside and keeps walking. The callback is only called for the final values, not for the inner arrays themselves.

Syntax

Call array_walk_recursive() with the values you need. Start with this line.

PHP
array_walk_recursive($data, function ($value, $key) { echo $value; });
CallWhat it does
array_walk_recursive($data, $callback)Visits every leaf value in nested arrays.
array_walk_recursive($data, function (&$v) { $v = trim($v); })Edits every value in place.
array_walk_recursive($data, $callback, "extra")Passes an extra argument to the callback.

Use it for tasks like trimming every string in nested form data, or summing all numbers in a nested structure.

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

Parameters

ParameterDescription
arrayRequired. The array (or object) to walk through. It is passed by reference.
callbackRequired. A function that receives the value, the key and optionally the extra argument. Use & on the value to modify it.
argOptional. An extra value passed to the callback as a third argument.

Return Value

Return ValueDescription
trueAlways returns true.

PHP Examples

Example 1 — Sum All Nested Numbers

Add up every number in a nested array.

PHP
<?php
$data = [1, [2, 3], [4, [5]]];
$total = 0;

array_walk_recursive($data, function ($value) use (&$total) {
    $total += $value;
});

echo "Total: ", $total, PHP_EOL;

How It Works

  1. The array has numbers at different nesting levels.
  2. use (&$total) lets the callback update the outside variable.
  3. array_walk_recursive() walks into each inner array and calls the callback for every number.
  4. 1 + 2 + 3 + 4 + 5 equals 15.

Example 2 — Only Leaf Values Are Visited

Compare the top-level count with the number of callback calls.

PHP
<?php
$data = ["a" => 1, "b" => ["c" => 2, "d" => ["e" => 3]]];
$visited = 0;

array_walk_recursive($data, function ($value, $key) use (&$visited) {
    $visited++;
});

echo "Top-level items: ", count($data), PHP_EOL;
echo "Leaf values visited: ", $visited, PHP_EOL;

How It Works

  1. The top level has two items: a and b. Item b is an array.
  2. The callback is not called for the arrays themselves.
  3. It runs only for the values 1, 2 and 3.
  4. So the counter is 3 even though count() says 2.

Example 3 — Trim Every String

Clean all text values in a nested form array.

PHP
<?php
$settings = ["name" => "  Ann ", "tags" => [" php ", " js"]];

array_walk_recursive($settings, function (&$value) {
    $value = trim($value);
});

echo json_encode($settings), PHP_EOL;

How It Works

  1. The array has strings with extra spaces, some in a nested list.
  2. The & on $value lets the callback change each item in place.
  3. trim() removes spaces from the start and end of every string.
  4. json_encode() shows the cleaned data.

Frequently Asked Questions

It applies a callback to every value in an array, including values inside nested arrays.
array_walk() only visits the top level. array_walk_recursive() goes inside nested arrays too.
No. It is only called for values that are not arrays.
Declare the first callback parameter by reference: function (&$value, $key).
Use a variable from outside, passed with use (&$variable), and update it inside the callback.

Next: arsort()

arsort() sorts an array by its values from highest to lowest and keeps the keys attached t.

arsort() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful