Home  >  Article  >  Backend Development  >  arrayadapter php array_walk array function

arrayadapter php array_walk array function

WBOY
WBOYOriginal
2016-07-29 08:46:02898browse

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 operation effect is as follows:
 php array_walk 数组函数

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn