PHP Array_Walk With Advanced Example

Definition and Usage

PHP has a built-in function called array_walk() which calls a user-defined function for each element in an array.

The function needs to know the keys and values of the array.

This goes through an entire array, no matter where the pointer is, and applies the callback function or a user-defined function to each element.

The internal array pointer of the array doesn’t change how the array_walk() function works.

Syntax:

array_walk( array, myfunction, parameter )

Parameters:

ParameterDescription
arraySpecification of array
myfunctionThe user-defined function’s name.
parameter,…Specifies a user-defined function parameter. We may assign one or several parameters to the function.

Technical Details

Return Value:Returns TRUE if it works and FALSE if it doesn’t.
PHP Version:4+

Errors/Exceptions

Since PHP 7.1.0, an ArgumentCountError is triggered if a callback function requires more than two parameters (the value and key of the array member) or more than three parameters if the arg is also given.

Previously, an error of level E_WARNING would be generated each time array_walk() called callback in this circumstance.

Example of array_walk()

By using a parameter:

<?php
    function myfunction( $value, $key, $p ){
        echo "$key $p $value<br>";
    }

    $array_sample = array( "A" => "Apple", "B" => "Banana", "C" => "Cucumber" );
    array_walk( $array_sample, "myfunction", "is equal to" );
?>

Output:

A is equal to Apple
B is equal to Banana
C is equal to Cucumber

The three arguments to the myfunction function are $value, $key, and $p. With these arguments, you can print the array values, the keys that go with them, and a short description.

The $array_sample variable is used to declare an array with specific keys and values.

Finally, an array_walk is used to call the user-defined function and display it in an array.

Change an array element’s value in array walk PHP

With the array_walk() function, we can change all of the elements in the array.

We need to send the &$value, not just the $value. Notice the & difference.

Example:

<?php
    echo("<pre>");

    function myfunction( &$value, $key ){
        $value = "Mango";
    }

    $array_sample = array( "A" => "Apple", "B" => "Banana", "C" => "Cucumber" );
    array_walk( $array_sample, "myfunction" );
    print_r($array_sample);

    echo("</pre>");
?>

Output:

Array
(
    [A] => Mango
    [B] => Mango
    [C] => Mango
)

In the above example, every value is replaced with the Mango value.

It is important to note that array_walk() cannot be used to modify array keys.

The array_walk() can be defined as (&$value, $key), but not as (&$value, & $key).

Additionally, PHP does not alter a key even if it does not complain or warn.

Even if there was declare (strict_types=1), PHP ignored the argument type when calling array_walk()

According to the code above, the <pre> tag is used to read the array conveniently and neatly.

Without the <pre> tag, the outcome is a one-line array that is difficult to understand and messy.

What is PHP array-walk?

PHP’s array_walk() method is a built-in function.

Regardless of the position of the pointer, the array_walk() function traverses the entire array and applies a callback function or user-defined function to each member.

The keys and values of the array elements are parameters for the callback function.

Moreover, array_walk() is not affected by the internal array pointer of array

The array_walk() will walk through the entire array regardless of the pointer position. Based on the PHP Documentation.

What are the 3 types of PHP arrays?

There are three types of arrays in PHP:

Summary

In summary, we learned how to use array_walk in PHP by analyzing example programs that demonstrated the different functions.

You can now utilize complex arrays in the coding of your own programs.

Lastly, if you want to learn more about PHP array_walk, please leave a comment below. We’ll be happy to hear it!

Leave a Comment