PHP: array operation functions array_walk () and array_map () function prototype: array
Array_map(Callback, array arr1 [, array...])
Array_map ()Returns an array containing all the units in arr1 that have passed the callback function.The number of parameters accepted by callback should beArray_map ()The number of arrays of the function is the same.
The callback function is the element unit function that array_map will call to process. the function name should be passed to array_map () in the form of a string ()
For example: (code provided in the php official manual)
Function cube ($ n)
{
Return ($ n * $ n );
}
$ A = array (1, 2, 3, 4, 5 );
$ B = array_map ("cube", $ );
Print_r ($ B );
?>
Cube is the callback function, and each of the callback function parameters of array_map should be an array element, that is, besides the first parameter, the remaining parameters should be arrays and should be the same as the number of callback parameters.
100) {return $ n-10;} else {return $ n ;}// callback 2 function add ($ a, $ B) {return $ a + $ B ;} $ arr = array (35,105, 99,109,); // var_dump ($ arr); $ brr = array ); $ arr = array_map ("check", $ arr); $ brr = array_map ("add", $ arr, $ brr); print_r ($ arr); echo"
"; Print_r ($ brr);?>
Output result
array (size=6) 0 => int 101 1 => int 85 2 => int 35 3 => int 105 4 => int 99 5 => int 109
Array ([0] => 91 [1] => 85 [2] => 35 [3] => 95 [4] => 99 [5] => 99)
Array ([0] => 92 [1] => 87 [2] => 38 [3] => 99 [4] => 104 [5] => 105)
========================================================== ========================================================== ==========
I am a split line
========================================================== ========================================================== ==========
Different from array_map (), the return value of array_walk () is Boolean, that is, if you want to modify the data of the arrayCallback functions (that is, references)
Array_walk () will pass the value of the array element and the key value to the callback function. In addition, it can also pass data of its type to the callback function.
The prototype and official documentation are as follows:
BoolArray_walk(Array & array, callback funcname [, mixed userdata])
If yes, returnTRUE, Returns if it fails.FALSE.
Apply the user-defined function funcname to each cell in the array. In typical cases, funcname accepts two parameters. The value of the array parameter is the first and the key name is the second. If the optional parameter userdata is provided, it is passed to callbackfuncname as the third parameter.
If the funcname function requires more parameters than the given oneArray_walk ()An E_WARNING error occurs when you call funcname. These warnings can beArray_walk ()Add the PHP error operator @ to suppress the call, or useError_reporting ().
Note:If funcname needs to act directly on the value in the array, specify the first parameter of funcname as a reference. In this way, any change to these units will also change the original array itself.
Note:Passing the key name and userdata to funcname is newly added in PHP 4.0.
Array_walk ()It is not affected by the array pointer inside the array.Array_walk ()It will traverse the entire array regardless of the pointer position.
You should not change the array in the callback function. For example, adding/deleting a unit or unset a unit. IfArray_walk ()If the array is changed, the behavior of this function is undefined and unpredictable.
Below is a simple example I wrote myself:
';} $ Arr = array ("a" => "A", "B" => "B", "c" => "C ", "d" => "D"); // array_walk -- apply the user function to each member in the array // Function prototype: bool array_walk (array & array, callback funcname [, mixed userdata]) // The first parameter is the array // The second parameter is the callback function name, and the third parameter is an optional parameter (which will be passed to the callback function) // ================================================ ========================================================== // array_map () the first parameter of the callback function is the value of the element. if you need to modify it, add the & operator // second parameter as the key value, if you need to modify it, perform the above Processing. // The third parameter is the data that the user passes to the function. (The third parameter in array_walk () array_walk ($ arr, 'alter ', ". is.");?>
Output
A. is.
B. is. B
C. is. C
D. is. D
Bytes ----------------------------------------------------------------------------------------
Bytes ----------------------------------------------------------------------------------------