Copy Code code as follows:
/* Function Array_walk (): Single array callback function---Apply user functions to each member of the log group
* 1, Syntax: bool Array_walk (array &array, callback funcname [, mixed $userdata])
* 2, Description: Return TRUE If successful, return FALSE if failed
* 3, the attention matters:
* 3.1, $funcname is the user-defined callback function, accept 2 parameters, the first parameter array $array value, the second parameter is
* The key name of the array $array, which is passed to the callback function as the third argument if the third parameter $userdata is supplied $funcname
* 3.2, the use of callback functions can directly change the values of each cell of the array, but change the key names are not valid
* 3.3, the function will not be affected by array pointers inside the array. Array_walk () traverses the entire array regardless of the pointer
* The position
* 3.4. The user should not change the array itself in the callback function, such as adding/removing units, unset units, and so on, if Array_walk ()
An array of effects is changed, the behavior of this function is undefined and unpredictable.
*/
$words =array ("L" => "Lemon", "O" => "Orange", "B" => "banana", "a" => "Apple");
Defines a callback function that outputs an array element
function Words_print ($value, $key, $prefix) {
echo "$prefix: $key => $value <br>\n";
}
Define a callback function to change the value of an element directly
Function Words_alter (& $value, $key) {
$value =ucfirst ($value);
$key =strtoupper (key);
}
The value of the output element
Array_walk ($words, ' words_print ', ' words ');
Change the value of an element
Array_walk ($words, ' words_alter ');
echo "<pre>";
Print_r ($words);
echo "</pre>";
The operation effect is as follows: