Function callback is a special mechanism in PHP. This mechanism allows you to input a user-defined function address in the function parameter list as a parameter to process or complete certain operations. You can use the callback function to easily display some required functions. The following describes the main functions that Use callback functions to process arrays.
① Function array_filter ()
The array_filter function uses the callback function to filter the elements in the array and return the new array filtered by the user-defined function. This function has two parameters. Its prototype is as follows:
Array array_filter (array input [, callback])
The first parameter of the function is required. You must enter a filtered array. The second parameter is optional, and the user-defined function name is passed in as a string. If the custom filter function returns true, the current value of the operated array is included in the returned array and the result is a new array. If the original array is an associated array, the key name remains unchanged. The code used by the array_filter () function is as follows:
123456789101112131415 |
<?php // Customize the function myfun to set conditions for Array Filtering function myFun( $var ){ if ( $var % 2 ==0) return true; } // An array with declared values as an integer Sequence $array = array ( "a" =>1, "b" =>2, "c" =>3, "d" =>4, "e" =>5); // Use the array_filter () function to pass the defined function name as a string to the second parameter. print_r( array_filter ( $array , "myfun" )); // The filtered result output array ([B] => 2 [d] => 4) ?> |
In the code above, the array_filter () function transmits each value in the $ array to the myfun () function at a time. If the myfun () function returns true, then, the current value of the $ array is included in the returned result array and the result is returned as a new array.
② Function array_walk ()
The array_walk () function applies the callback function to each element in the array. If the call succeeds, true is returned. Otherwise, false is returned. The function prototype is as follows:
Bool array_walk (array $ array, callback funcname [, mixed userdata])
The first parameter of the function is required. You must enter an array to be processed by the specified callback function. The second parameter is also required to pass in the user-defined callback function, which is used to input the array of the first parameter. The array_walk () function transmits each value in the array of the first parameter to this custom function at a time. The user-defined callback function should receive two parameters. The values of the imported elements are used as the first parameter, and the key name is used as the second parameter. The optional third parameter provided in the array_walk () function will also be received as the third parameter of the callback function.
If the custom callback function requires more parameters than the given ones, an e_warning error is generated every time array_walk () calls the callback function. These warnings can be suppressed by adding the PHP error operator @ before the array_walk () call, or error_reporting ().
If the callback function needs to directly act on the values in the array, you can specify the first parameter of the callback function as reference: & $ value. The code used by the array_walk () function is as follows:
1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
<?php // Define a function that can be called back. Its name is myfun1. function myfun1( $value , $key ) { echo "The key $key has the value $value<br>" ; } // Define an array $ Lamp $lamp = array ( "a" => "Linux" , "b" => "Apache" , "c" => "MySQL" , "d" => "PHP" ); // Use the array_walk () function to input an array and a callback function array_walk ( $lamp , "myfun1" ); /* The output after execution is as follows: The key $key has the value Linux The key $key has the value Apache The key $key has the value MySQL The key $key has the value PHP */ // Define a function that can be called back. Its name is myfun2. function myfun2( $value , $key , $p ){ echo "$key $p $value <br>" ; } // Use the array_walk function to input three parameters array_walk ( $lamp , "myfun2" , "has the value" ) /* Output the following results after execution: a has the value Linux b has the value Apache c has the value MySQL d has the value PHP */ // Define a function that can be called back and its name is myfun3. Change the value of the array element. function myfun3(& $value , $key ){ $value = "web" // The value of each element in the original array will be changed } // Use the array_walk () function to input two parameters. The first parameter is a reference. array_walk ( $lamp , "myfun3" ); print_r( $lamp ); // Output: array ([a] => Web [B] => Web [c] => Web [d] => Web) ?> |
③ Function array_map ()
Compared with the previous array_walk () function, the array_map () function is more flexible and can process multiple arrays. The callback function acts on the elements of the given array and returns the array after the User-Defined Function acts. Array_map () is an arbitrary parameter list function. The number of parameters received by the callback function must be the same as the number of arrays passed to the array_map () function. The function prototype is as follows:
Array array_map (callback, array arr1 [, array...])
The first parameter in the function is required. It is the name of the user-defined callback function or null. The second parameter is required. Enter the array to be processed. You can also enter multiple arrays as optional parameters. The code used by the array_map () function is as follows:
1234567891011121314151617181920212223242526272829303132333435363738394041 |
<?php // Customize a function as the callback function. The function name is myfun1. function myfun1( $v ) { if ( $v === "MySQL" ){ // If the element value in the array is equal to the MySQL Condition return "Oracle" ; // Return to Oracle } return $v ; // All elements not equal to MySQL return input values } // Declare an array with four elements $ Lamp $lamp = array ( "Linux" , "Apache" , "MySQL" , "PHP" ); // Use the array_map () function to input a function name and an array parameter. print_r( array_map ( "myfun1" , $lamp )); // Output array after the above program is executed ([0] => Linux [1] => Apache [2] => Oracle [3] => PHP) // Declare that a function uses multiple parameters. The number of parameters received by the callback function must be the same as the number of parameters passed to the array_map () function, // To customize a function, the elements in the two arrays must be input in sequence. function myfun2( $v1 , $v2 ) { if ( $v1 === $v2 ){ // If the element values in the two arrays are the same, the condition is true. return "same" ; } return "different" ; } $a1 = array ( "Linux" , "PHP" , "MySQL" ); // Declare an array $ A1 with three elements $a2 = array ( "Unix" , "PHP" , "Oracle" ); print_r( array_map ( "myfun2" , $a1 , $a2 )); // Use the Array () function to input multiple arrays // Output after the above program is executed: array ([0] => different [1] => same [2] => different) // When the custom function name is set to null $a1 = array ( "Linux" , "Apache" ); $a2 = array ( "MySQL" , "PHP" ); print_r( array_map (null, $a1 , $a2 )); // Output after the above program is executed: array ([0] => array ([0] => Linux [1] => MySQL) [1] => array ([0] => Apache [1] => PHP )) ?> |
Generally, when the array_map () function uses two or more arrays, their length should be the same, because the callback function works in parallel on the corresponding unit. If the length of the array is different, the shortest one will be expanded with an empty unit.
> Fixed link: http://php.ncong.com/php_course/arry_function/array_huidiao.html
> For Reprinted Information, please note: ncong PHP was published in ncong PHP learning tutorial on September 10, April 13, 2014.