The php array_walk() function gives us the ability to apply a function to all elements in an array. array_walk() modifies the passed array if the function parameter is passed by reference

function square(&$var) {
    $var =  $var * $var;
}

$array = array(1,2,3,4,5);

array_walk($array, "square");

print_r($array);

// Output:
Array
(
    [0] => 1
    [1] => 4
    [2] => 9
    [3] => 16
    [4] => 25
)

When working with collections of data in php, the ability to easily access and manipulate the elements of arrays.

One such operation is being able to apply a function to each element of an array.

The php array_walk() function gives us the ability to apply functions to arrays. The callback function will be applied to each element of an array, and the array will be modified in place.

Below is a basic example showing how you can use array_walk() to square each element of an array of numbers in php.

function square(&$var) {
    $var =  $var * $var;
}

$array = array(1,2,3,4,5);

array_walk($array, "square");

print_r($array);

// Output:
Array
(
    [0] => 1
    [1] => 4
    [2] => 9
    [3] => 16
    [4] => 25
)

You can also define and pass the callback function directly to array_walk().

Below is another example of how you can define and pass a callback function to array_walk().

$array = array(1,2,3,4,5);

array_walk($array, function(&$var) {
    return $var * $var;
});

print_r($array);

// Output:
Array
(
    [0] => 1
    [1] => 4
    [2] => 9
    [3] => 16
    [4] => 25
)

Passing a Third Parameter to the Callback Function with array_walk()

The php array_walk() function has two required parameters; the array to work on and a callback function.

There is a third optional parameter which can be passed to the callback function.

For example, let’s say we have an array of numbers and want to multiply all of them by another number.

We can do that with array_walk() by utilizing this third parameter.

Below is an example of how to use array_walk() to pass an argument to the callback function.

function multiply(&$val, $key, $num) {
    $val =  $val * $num;
}

$array = array(1,2,3,4,5);

array_walk($array, "multiply", 5);

print_r($array);

// Output:
Array
(
    [0] => 5
    [1] => 10
    [2] => 15
    [3] => 20
    [4] => 25
)

The Difference between array_map() and array_walk() in php

When I was learning about array_walk() and the php array_map() function for the first time, I was confused since it seemed to me like they did the same thing.

There are a few differences between array_map() and array_walk().

The main difference is that array_walk() operates on only one array and does not return a new array. This is different from array_map() as we can pass multiple arrays to array_map() and create a new array.

Depending on the situation, it may be more beneficial to use one function over the other. But in general, array_walk() is more resource efficient when working with one array.

Hopefully this article has been useful for you to learn how to use the php array_walk() function to apply functions to arrays in php.

Categorized in:

PHP,

Last Update: February 26, 2024