PHP array_walk () function definition and usage
The PHP Array_walk () function applies a callback function to each element in the array. Returns TRUE if successful, otherwise FALSE.
Typically, a function accepts two parameters. The value of the array parameter as the first, the key name as the second. If an optional parameter userdata is provided, it is passed as the third argument to the callback function.
If the function requires more arguments than is given, a e_warning-level error is generated each time Array_walk () invokes the function. These warnings can be suppressed by adding the PHP error operator @ before the Array_walk () call, or by using error_reporting ().
PHP array_walk () function syntax
Array_walk (Array,function,userdata ...)
PHP Array_walk () function parameters and descriptions
Array required. Specifies the array.
function Required. The name of the user-defined function.
UserData is optional. The value entered by the user, which can be used as a parameter to the callback function.
PHP Array_walk () function hints and comments
Tip: You can set one or more parameters for a function.
Note: If the callback function needs to directly act on the values in the array, you can specify the first parameter of the callback function as a reference to the:& $value. (see example 3)
Note: Passing the key name and UserData to the function is a new addition to PHP 4.0.
PHP Array_walk () Function Example 1
- < ? PHP
- function MyFunction ($value, $key)
- {
- echo "The key $key has the value
$value<br/>";
- }
- $ a = Array ("A" =>"Cat", "b" =>"Dog",
"C" =>"Horse");
- Array_walk ($a, "myfunction");
- ?>
Output:
The key A has the value Cat
The key B has the value Dog
The key C has the value Horse
http://www.bkjia.com/PHPjc/446073.html www.bkjia.com true http://www.bkjia.com/PHPjc/446073.html techarticle the PHP array_walk () function defines and uses the PHP Array_walk () function to apply a callback function to each element in the array. Returns TRUE if successful, otherwise FALSE. Typical case of funct ...