PHP Learning Note array_walk () function

Source: Internet
Author: User

Sometimes, readers may want to use or modify each element of an array in the same way. The Array_walk () function allows such an operation. The prototype of the function Array_walk () is as follows:

bool Array_walk (array arr,string func,[mixed UserData])

The calling method is similar to the call of the Usort () function described earlier, and the Array_walk () function requires that you declare a function of your own. As you can see, the Array_walk () function requires three parameters. The first is arr, which is the array that needs to be processed. The second is the Func, which is the function that the user customizes and will act on each element in the array.

The third parameter, UserData, is optional, and if it is used, it can be passed as a parameter to our own function.

In the next section, the reader will understand how this function works.

This user-defined function can be a function that displays individual elements in a specified format. The code shown below displays each element in a new row by invoking the user-defined My_print () function in each element of the $array array

function My_print ($value) {

echo "$value
";

}

Array_walk ($array, ' my_print ');

This function, written above, also requires a specific symbol. For each element in the array, Array_walk will be the parameter with the keyword and the value saved in the array, and you can also use any data as the UserData parameter of the function. You can call a function as follows:

yourfunction (Value,key,userdata);

In most cases, a function can only handle values in an array. However, in some cases, you may also need to pass a parameter to the function using the UserData parameter. In rare cases, you may also need to handle the array keywords and values. In the Myprint () function, you can omit the keyword argument and the UserData parameter.

In a slightly more complex example, you will write a function with a parameter that can be used to modify the value of the array. Note that although the keyword is not of interest, it must be received in order to receive the third argument variable:

Function my_multiply (& $value, $key, $factor) {

$value *= $factor;

}

Array_walk (& $array, ' my_multiply ', 3);

Here, a function named My_multiply () is defined, which can be multiplied by the provided multiplication factor to multiply each element in the array. You need to use the third parameter of the Array_walk () function to pass the multiplication factor. Because this parameter is required, the my_multiply () function must be defined with three parameters-an array element value ($value), a keyword for an array element ($key), and a parameter ($factor). You can choose to ignore this keyword.

In addition, there is a need to pay attention to the problem is the way to pass the parameter $value. In the function definition of my_multiply (), the address character in front of the variable (&) means that the $value is passed by reference. Passing by reference allows the function to modify the contents of an array.

PHP Learning Note array_walk () function

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.