AppDividend
Latest Code Tutorials

PHP array_walk Example | PHP array_walk() Function Tutorial

0

PHP array_walk Example | PHP array_walk() Function Tutorial is today’s topic. The array_walk() function runs each array item in the user-defined function. The user-defined function takes an array’s values and keys as parameters. PHP array_walk() is not affected by the internal array pointer of the array. The array_walk() will walk through the entire array regardless of pointer position. You can change the array element’s value in the user-defined function by specifying the first parameter as a reference.

PHP array_walk Example

PHP array_walk() function is the inbuilt function in PHP. The array_walk() function walks through an entire array regardless of pointer position and applies the callback function or user-defined function(UDF) to every item of the array. See the below syntax.

array_walk(array,myfunction,parameter...)

The array parameter is required and specifying an array.

The myfunction parameter is required and the name of a user-defined function.

The parameter is optional, and it specifies the parameter to the user-defined function(UDF). You can assign one parameter to a function, or as many as you like.

See the following example.

<?php

// app.php

function demo($value, $key, $p)
{
  echo "$key $p $value"."\n";
}

$MVP = ['Arya', 'Jon', 'Sansa', 'Tyrion', 'Bran'];
array_walk($MVP, "demo", "has the value");

See the below output.

PHP array_walk Example

 

The function returns the boolean value. It returns TRUE value on success or FALSE on failure.

Change an array element’s value

We can change all the elements of the array using array_walk() function.

For that, we need to pass the &$value instead of just $value. Notice the & difference.

See the following example.

<?php

// app.php

function demo(&$value, $key)
{
  $value = 'Hound';
}

$MVP = ['Arya', 'Jon', 'Sansa', 'Tyrion', 'Bran'];
array_walk($MVP, "demo", "has the value");
print_r($MVP);

See the output below.

Change an array element's value

 

In the above example, all the values are replaced by Hound value. 

It’s worth noting that array_walk cannot be used to change the keys in an array. The array_walk() function may be defined as (&$value, $key) but not (&$value, &$key). Even though PHP does not complain or warn, it does not modify a key. PHP ignored the arguments type when using the array_walk() even if there was declare(strict_types=1). 

If you want to unset items from a callback function, maybe what you need is array_filter. The array_walk() function cannot modify / change / reindex keys.

It can be advantageous to pass the third (optional) parameter by reference while modifying it permanently in the callback function. This will cause passing the modified parameter to next iteration of array_walk().

Conclusively, PHP array_walk Example | PHP array_walk() Function Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.