PHP array_walk () array function _php tips

Source: Internet
Author: User
Copy Code code as follows:

/* Function Array_walk (): Single array callback function---Apply user functions to each member of the log group
* 1, Syntax: bool Array_walk (array &array, callback funcname [, mixed $userdata])
* 2, Description: Return TRUE If successful, return FALSE if failed
* 3, the attention matters:
* 3.1, $funcname is the user-defined callback function, accept 2 parameters, the first parameter array $array value, the second parameter is
* The key name of the array $array, which is passed to the callback function as the third argument if the third parameter $userdata is supplied $funcname
* 3.2, the use of callback functions can directly change the values of each cell of the array, but change the key names are not valid
* 3.3, the function will not be affected by array pointers inside the array. Array_walk () traverses the entire array regardless of the pointer
* The position
* 3.4. The user should not change the array itself in the callback function, such as adding/removing units, unset units, and so on, if Array_walk ()
An array of effects is changed, the behavior of this function is undefined and unpredictable.
*/
$words =array ("L" => "Lemon", "O" => "Orange", "B" => "banana", "a" => "Apple");
Defines a callback function that outputs an array element
function Words_print ($value, $key, $prefix) {
echo "$prefix: $key => $value <br>\n";
}
Define a callback function to change the value of an element directly
Function Words_alter (& $value, $key) {
$value =ucfirst ($value);
$key =strtoupper (key);
}
The value of the output element
Array_walk ($words, ' words_print ', ' words ');
Change the value of an element
Array_walk ($words, ' words_alter ');
echo "<pre>";
Print_r ($words);
echo "</pre>";

The operation effect is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.