This article describes how to use the PHParray_walk () function. The PHParray_walk () function defines and uses the PHParray_walk () function to apply a callback function to each element in the array. If the call succeeds, TRUE is returned. otherwise, FALSE is returned. Funct in typical cases
PHP array_walk () function definition and usage
The PHP 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 ().
PHP array_walk () function syntax
Array_walk (array, function, userdata ...)
PHP array_walk () function parameters and descriptions
Array is required. Specified array.
Required. The name of the user-defined function.
Userdata is optional. The value that you enter can be used as a callback function parameter.
PHP array_walk () function prompts 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 of the PHP array_walk () function
- < ?php
- function myfunction($value,$key)
- {
- echo "The key $key has the value
$value
";
- }
- $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.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446073.htmlTechArticlePHP array_walk () function definition and usage PHP 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, funct...