php array_walk() array function_PHP tutorial

WBOY
Release: 2016-07-21 15:27:04
Original
927 people have browsed it

Copy code The code is as follows:

/*Function array_walk(): Single array callback function---for each member in the array Apply user function
* 1. Syntax: bool array_walk ( array &array, callback funcname [, mixed $userdata] )
* 2. Description: Return TRUE if successful, return FALSE if failed
* 3. Notes:
* 3.1. $funcname is a callback function defined by the user. It accepts 2 parameters. The first parameter is the value of the array $array, and 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. Using the callback function can 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 array. array_walk() will traverse the entire array regardless of the position of the pointer
*
* 3.4. The user should not change the array itself in the callback function, such as adding/deleting cells, unset cells, etc., if array_walk()
* If the array it acts on changes, 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}
//Define a callback function to directly change 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() array function_PHP tutorial

http://www.bkjia.com/PHPjc/323852.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323852.htmlTechArticleCopy the code as follows: /*Function array_walk(): Single array callback function---for each element in the array Apply user function to each member* 1. Syntax: bool array_walk (array gt;"lemon","o"="oran...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template