PHP array_walk() function

PHP array_unshift() function
PHP array_walk_recursive() function

The array_walk function iterates through an array of elements and passes each element’s key and value to a user-defined function. The keys and values of the array are the parameters of the user-defined function.

We can change the value in the user-defined function by passing the first parameter ($value) as reference (&$value).

What is the syntax of the array_walk function in PHP?

array_walk(array, myfunction, parameter...)
ParametersDetails
arrayThe array to walking through – Required
my_functionThe user-defined function for array keys and values – Required
parameter,…Defines the parameters for the user-defined function – Optional
array_walk function in PHP

Examples of array_walk function

Example 1. Example of array_walk with parameter.

<?php
function my_function($value,$key,$param)
{
echo "$key $param $value";
}
$arr=array("a"=>"R","b"=>"G","c"=>"B");
array_walk($arr,"my_function","contains value");
?>

Example 2. Change the array element value within the array_walk user-defined function.

<?php
function my_function(&$value,$key)
{
$value="Red";
}
$arr=array("a"=>"R","b"=>"G","c"=>"B");
array_walk($arr,"my_function");
print_r($arr);
?>
PHP array_unshift() function
PHP array_walk_recursive() function
en English
X
Scroll to Top