Php array_walk () Array Function

Source: Internet
Author: User

Copy codeThe Code is as follows:/* function array_walk (): single array callback function --- apply the User Function 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 <br> \ 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 "<pre> ";
Print_r ($ words );
Echo "</pre> ";

The running 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.