Backend Development 6 min read

Using PHP's array_walk() Function: Syntax, Basic and Advanced Examples

This article explains PHP's array_walk() function, detailing its syntax, parameters, and demonstrating basic usage for element transformation, advanced techniques with userdata, and integration with class methods, while providing practical code examples and real‑world application scenarios.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP's array_walk() Function: Syntax, Basic and Advanced Examples

The array_walk() function is a powerful PHP utility that lets developers iterate over an array and execute a custom operation on each element.

Its simple syntax is:

<code>array_walk($array, $callback, $userdata);</code>

where $array is the array to traverse, $callback is the function to invoke for each element, and $userdata is an optional parameter that can pass extra data to the callback.

Below we explore the usage of array_walk() with examples.

Basic usage : Suppose we have a numeric array and want to square each element. We can achieve this with array_walk() as follows:

<code>$numbers = array(1, 2, 3, 4, 5);
function square($value, $key) {
    $value = $value * $value;
    echo "The square of $key is $value\n";
}
array_walk($numbers, 'square');
</code>

In this example, the square function is defined as the callback; it receives each $value and its $key , computes the square, and prints the key and result.

The output of the script is:

<code>The square of 0 is 1
The square of 1 is 4
The square of 2 is 9
The square of 3 is 16
The square of 4 is 25
</code>

From the output we see that array_walk() applied the square operation to every array element and displayed the key and squared value.

Advanced usage : The $userdata parameter can be used to pass additional data into the callback. For example, to calculate the sum of all elements we can write:

<code>$sum = 0;
function sum($value, $key, $userdata) {
    $sum = $userdata;
    $sum += $value;
    return $sum;
}
$numbers = array(1, 2, 3, 4, 5);
$sum = array_walk($numbers, 'sum', $sum);
echo "The sum of all numbers is $sum";
</code>

Here $userdata carries the running total ( $sum ) into the callback, which adds each element's value and returns the updated sum.

Note that if you need to modify $userdata inside the callback, you must pass it by reference; otherwise the changes will not persist.

Using a class method : array_walk() can also invoke a method of an object. Consider the following class:

<code>class MyClass {
    public function filter($value, $key) {
        // filtering code here
    }
}
</code>

We can call the method with:

<code>$myClass = new MyClass();
$array = array('a', 'b', 'c', 'd', 'e');
array_walk($array, array($myClass, 'filter'));
</code>

This example shows how to instantiate MyClass , store it in $myClass , and then use its filter method as the callback for array_walk() .

Real‑world applications : The array_walk() function is useful in many practical scenarios, such as parsing log files, formatting or validating database fields, filtering request parameters, and more. Its ability to apply a custom operation to each array element can greatly simplify code and improve readability.

backendPHPcallbackphp-functionsarray_walkuserdata
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.