Comparison between array_map and array_walk in php I. array_map ()
1. the array_map () function applies a user-defined function to each value in the array, and returns an array with a new value after the user-defined function is applied, if no value is returned after the function is applied, the corresponding new value array is null.
2. the number of parameters accepted by the callback function should be the same as the number of arrays passed to the array_map () function.
3. Tip: you can input one or more arrays to the function.
If the phase function inputs two arrays, the function acceptance parameters should also have two. when map is passed to the function, each time one of the two arrays is retrieved and passed to the function.
Number. That is to say, multiple arrays are submitted synchronously. if you do not submit an array and then submit several arrays, the function also requires several parameters.
4. Syntax: array array_map (callable callback, arrayarr1 [, array $... ])
Instance:
2. array_walk ()
1. array_walk-use a user-defined function to callback each element in the array
2. Syntax: bool array_walk (array & array, callablefuncname [, mixed $ userdata = NULL])
Parameters:
$ Array: the input array.
$ Funcname: the callback function. Typically, $ funcname accepts two parameters. $ The value of the array parameter is the first and the key name is the second.
$ Userdata if the optional parameter $ userdata is provided, it will be passed to $ funcname as the third parameter.
Note:
If $ funcname needs to act directly on the value in the array, specify the first parameter of funcname as reference (Add & symbol ). This way
Any change to these cells will also change the original array itself.
Return value:
Returns TRUE if the call succeeds, or FALSE if the call fails.
Instance:
"Lemon", "a" => "orange", "B" => "banana", "c" => "apple"); // upload a reference, function test_alter (& $ item1, $ key, $ prefix) {$ item1 = "$ prefix: $ item1";} function test_print ($ item2, $ key) {echo "$ key. $ item2
\ N ";} echo" Before...: \ n "; // a single array of array_walk ($ fruits, 'Test _ print'); // array_walk with additional parameters ($ fruits, 'Test _ alter ', 'fruit'); echo "... and after: \ n "; array_walk ($ fruits, 'Test _ print ');
The above routine will output:
Before ...:d. lemona. orangeb. bananac. apple ... and after:d. fruit: lemona. fruit: orangeb. fruit: bananac. fruit: apple
Key points:
Map is mainly used to obtain the new array processed by your callback function. The result is required.
The walk mainly uses your callback function for each parameter. The process is required.
Walk can be considered to provide additional parameters to the callback function. map cannot.
A walk performs operations on each value in the logarithm group. The operation results affect the original array.
Map is mainly used to operate the values in the array and then return an array to get a new array.
The walk can have no returned value. The map must have a value because the array needs to be filled.