PHP array_walk() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_walk() runs a function on every item of an array.

Your callback receives the value and the key of each item. If the callback takes the value by reference, it can change the array in place.

Syntax

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

PHP
array_walk($fruits, function ($value, $key) { echo $key; });
CallWhat it does
array_walk($arr, function ($v, $k) { ... })Calls the function for each value and key.
array_walk($arr, function (&$v, $k) { $v = $v * 2; })Changes each value using a reference.
array_walk($arr, $callback, "extra")Passes an extra argument to the callback.

array_walk() is meant for doing something with each item or changing items in place. If you want a new array built from the results, array_map() is usually a better fit.

Library declarations
PHP
array_walk(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. Put & before the value parameter to modify it.
argOptional. An extra value passed as the third argument to the callback.

Return Value

Return ValueDescription
trueAlways returns true. The useful work happens inside the callback.

PHP Examples

Example 1 — Visit Each Item

Print every key and value in an array.

PHP
<?php
$fruits = ["a" => "apple", "b" => "banana"];

array_walk($fruits, function ($value, $key) {
    echo $key, " = ", $value, PHP_EOL;
});

How It Works

  1. Create an associative array of two fruits.
  2. array_walk() calls the function once for each item.
  3. The callback receives the value first and the key second.
  4. It prints each pair on its own line.

Example 2 — Change Values by Reference

Use & on the value parameter to edit the array in place.

PHP
<?php
$prices = [1, 2, 3];

$result = array_walk($prices, function (&$price, $key) {
    $price = $price * 2;
});

echo implode(", ", $prices), PHP_EOL;
var_dump($result);

How It Works

  1. The & before $price gives the callback a reference to the real array item.
  2. Doubling $price changes the original array.
  3. The array now holds 2, 4 and 6.
  4. array_walk() itself returns true, which var_dump() shows.

Example 3 — Pass an Extra Argument

The third argument of array_walk() is handed to the callback as its third parameter.

PHP
<?php
$names = ["ann", "ben"];

array_walk($names, function (&$value, $key, $prefix) {
    $value = $prefix . ucfirst($value);
}, "Hi, ");

echo implode(" | ", $names), PHP_EOL;

How It Works

  1. The string "Hi, " is passed as the extra argument.
  2. The callback receives it as $prefix.
  3. ucfirst() capitalizes each name and the prefix is added in front.
  4. implode() joins the finished greetings with a bar.

Frequently Asked Questions

It calls a function on every item in an array, passing the value and the key. It can also change the array in place.
array_map() returns a new array of results. array_walk() works on the original array and returns true.
Declare the value parameter with a reference, like function (&$value, $key). Changes to $value then update the array.
No. It only visits the top level. Use array_walk_recursive() to reach nested arrays.
It returns true.

Next: array_walk_recursive()

array_walk_recursive() runs a function on every item of an array, including items inside n.

array_walk_recursive() 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