Function array_walk (): a single array callback function-applies user functions to each member in the array.
The 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
\ 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"
";
print_r($words);
echo "
";
The running effect is as follows: