Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP array_walk() Function

PHP array_walk() Function

PHP Array ReferenceComplete PHP Array Reference Manual

Example

Apply a user-defined function to every element of an array:

"; } $a=array("a"=>"red","b"=>"green","c"=>"blue"); array_walk($a,"myfunction"); ?>

Run Example »


Definition and Usage

The array_walk() function applies a user-defined function to every element of an array. In the function, the array key and value are the parameters.

**Note:** You can change the values of array elements by specifying the first parameter of the user-defined function as a reference: &$value (see Example 2).

**Tip:** To operate on deeper arrays (an array containing another array), use the array_walk_recursive() function in Vue 3.


Syntax

array_walk(

array,myfunction,parameter...

)

Parameters Description
array Required. Specifies the array.
myfunction Required. The name of the user-defined function.
parameter,... Optional. Specifies parameters for the user-defined function; you can set one or more parameters for the function.

Technical Details

Return Value: Returns TRUE on success, FALSE on failure.
PHP Version: 4+

More examples

Example 1

With one parameter:

"; } $a=array("a"=>"red","b"=>"green","c"=>"blue"); array_walk($a,"myfunction","has the value"); ?>

Run Example »

Example 2

Change the values of array elements (note &$value):

"red","b"=>"green","c"=>"blue"); array_walk($a,"myfunction"); print_r($a); ?>

Run Example »


PHP Array ReferenceComplete PHP Array Reference Manual