PHP | array_walk() Function

The array_walk() function is an inbuilt function in PHP. The array_walk() function walks through the entire array regardless of pointer position and applies a callback function or user-defined function to every element of the array. The array element’s keys and values are parameters in the callback function.

Syntax:

boolean array_walk($array, myFunction, $extraParam)

Parameters: This function accepts three parameters as described below:



  1. $array: This is a mandatory parameter and specifies the input array.
  2. myFunction: This parameter specifies the name of the user-defined function and is also mandatory. The user-defined function generally excepts two parameters of which the first parameter represent the array’s values and the second parameter represents the corresponding keys.
  3. $extraparam: This is an optional parameter. It specifies an extra parameter to the user-defined function in addition to the two parameters, array keys and values.

Return value: This function returns a boolean value. It returns TRUE on success or FALSE on failure.

Below programs illustrate the array_walk() function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to illustrate array_walk()
// function
  
// user-defined callback function
function myfunction($value, $key)
{
    echo "The key $key has the value $value \n";
}
  
// Input array
$arr = array("a"=>"yellow", "b"=>"pink", "c"=>"purple");
  
// calling array_walk() with no extra parameter
array_walk($arr, "myfunction");
  
?>

chevron_right


Output:

The key a has the value yellow 
The key b has the value pink 
The key c has the value purple 

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to illustrate array_walk()
// function
  
// user-defined callback function
function myfunction($value, $key, $extraParam)
{
    echo "The key $key $extraParam $value \n";
}
  
// Input array
$arr = array("a"=>"yellow", "b"=>"pink", "c"=>"purple");
  
// calling array_walk() with extra parameter
array_walk($arr, "myfunction", "has the value");
  
?>

chevron_right


Output:

The key a has the value yellow 
The key b has the value pink 
The key c has the value purple 

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to illustrate array_walk()
// function
  
// user-defined callback function to 
// update array values - to update array 
// values, pass the first parameter by reference
function myfunction(&$value, $key)
{
    $value = $value + 10;
}
  
// Input array
$arr = array("first"=>10, "second"=>20, "third"=>30);
  
// calling array_walk() with no extra parameter
array_walk($arr, "myfunction");
  
// printing array after updating values
print_r($arr);
  
?>

chevron_right


Output:

Array
(
    [first] => 20
    [second] => 30
    [third] => 40
)

Reference:
http://php.net/manual/en/function.array-walk.php



My Personal Notes arrow_drop_up

Contented with little yet wishing for more

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.