PHParray_walk () function definition and usage
The array_walk () function applies a callback function to each element in the array. If the call succeeds, TRUE is returned. otherwise, FALSE is returned.
In typical cases, a function accepts two parameters. The value of the array parameter is the first and the key name is the second. If the optional parameter userdata is provided, it is passed to the callback function as the third parameter.
If the function requires more parameters than the given parameter, an E_WARNING error is generated every time array_walk () calls the function. These warnings can be suppressed by adding the PHP error operator @ Before the array_walk () call, or error_reporting ().
Syntax
array_walk(array,function,userdata...)
Parameter description
| Array |
Required. Specified array. |
| Function |
Required. The name of the user-defined function. |
| Userdata |
Optional. The value that you enter can be used as a callback function parameter. |
Tips and comments
Tip: You can set one or more parameters for a function.
Note: If the callback function needs to act directly on the values in the array, you can specify the first parameter of the callback function as reference: & $ value. (See Example 3)
Note: passing the key name and userdata to the function is newly added to PHP 4.0.
Example 1
";}$a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");array_walk($a,"myfunction");?>
Output:
The key a has the value CatThe key b has the value DogThe key c has the value Horse
Example 2
With a parameter:
";}$a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");array_walk($a,"myfunction","has the value");?>
Output:
a has the value Catb has the value Dogc has the value Horse
Example 3
Change the value of an array element (note & $ value ):
"Cat","b"=>"Dog","c"=>"Horse");array_walk($a,"myfunction");print_r($a);?>
Output:
Array ( [a] => Bird [b] => Bird [c] => Bird )