Home > Article > Backend Development > arrayadapter php array_walk array function
Copy code The code is as follows:
/*Function array_walk(): Single array callback function---Apply user function to each member in the array
* 1. Syntax: bool array_walk (array &array, callback funcname [, mixed $userdata] )
* 2. Description: Returns TRUE if successful, FALSE if failed
* 3. Notes:
* 3.1. $funcname is a callback function defined by the user, accepting 2 parameters. The first parameter is the value of the array $array, the second parameter is
* the key name of the array $array, if the third parameter $userdata is provided, it will be passed to the callback function $funcname as the third parameter
* 3.2. You can use the callback function to directly change the value of each unit of the array, but changing each key name is invalid
* 3.3. This function will not be affected by the internal array pointer of the array. array_walk() will traverse the entire array regardless of the position of the pointer
*
* 3.4. Users should not change the array itself in the callback function, such as adding/deleting cells, unset cells, etc., if array_walk()
* acts on the array If changed, the behavior of this function is undefined 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";
}
//Definition A callback function directly changes the value of the element
function words_alter(&$value,$key){
$value=ucfirst($value);
$key=strtoupper(key);
}
//Output the value of the element
array_walk ($words,'words_print','words');
//Change the value of the element
array_walk($words,'words_alter');
echo "
"; <br>print_r($words); <br>echo "";
The above introduces the arrayadapter php array_walk array function, including the content of arrayadapter. I hope it will be helpful to friends who are interested in PHP tutorials.