Copy codeThe Code is as follows:
/* Function array_walk (): single array callback function-applies user functions to each member in the array
* 1. Syntax: bool array_walk (array & array, callback funcname [, mixed $ userdata])
* 2. Description: TRUE is returned if the call succeeds, and FALSE is returned if the call fails.
* 3. Notes:
* 3.1 and $ funcname are user-defined callback functions. Two parameters are accepted. The first parameter is the value of array $ array, and the second parameter is
* Array $ array key name. If the third parameter $ userdata is provided, it will be passed as the third parameter to the callback function $ funcname
* 3.2 using the callback function, you can directly change the values of each cell in the array, but changing the key names is invalid.
* 3.3. This function is not affected by array pointers in the array. Array_walk () traverses the entire array regardless of the pointer.
* Location
* 3.4 the user should not change the array itself in the callback function, for example, adding/deleting a unit or unset A unit. If array_walk ()
* If the array is changed, the behavior of this function is not defined and unpredictable.
*/
$ Words = array ("l" => "lemon", "o" => "orange", "B" => "banana ", "a" => "apple ");
// Define a callback function to output array elements
Function words_print ($ value, $ key, $ prefix ){
Echo "$ prefix: $ key = >$ value <br> \ n ";
}
// Define a callback function to directly change the element value
Function words_alter (& $ value, $ key ){
$ Value = ucfirst ($ value );
$ Key = strtoupper (key );
}
// Output element value
Array_walk ($ words, 'words _ print', 'word ');
// Change the element value
Array_walk ($ words, 'words _ alter ');
Echo "<pre> ";
Print_r ($ words );
Echo "</pre> ";
The running effect is as follows: