The function prototype for Array_map () is: array array_map (callback callback, array arr1 [, array ...])
array_map () returns an array that contains all the cells in the cell after it has been arr1 callback worked. callback The number of arguments accepted should be the same as the number of arrays passed to the array_map () function.
the callback function is called by ARRAY_MAP to process the element cell function, and the function name should be passed to Array_map () as a string.
such as: (Code given in the official PHP manual)
<?php
functionCube($n)
{
Return ($n* $n* $n);
}
$a= Array (1, 2, 3, 4, 5);
$b= Array_map("Cube", $a);
Print_r($b);
?>
cube即为callback函数 ,且array_map的callback函数的参数每一个应均为数组的元素,也就是array_map()除了第一个参数外,其余参数应均为数组且应与回调函数的参数个数一样.
<?php//callback 1function Check ($n) {//array_mapcallback parameter is an array of elements,//That is, callback has several parameters Array_map should pass in several arrays if ($n >100) {return $n-10;} else {return $n;}} Callback 2function Add ($a, $b) {return $a + $b;} $arr = Array (101,85,35,105,99,109),//var_dump ($arr), $BRR = Array (1,2,3,4,5,6), $arr = Array_map ("Check", $arr); $brr = Array_map ("Add", $arr, $BRR);p Rint_r ($arr); echo "<br/>";p Rint_r ($BRR);? >
Output results
Array (size=6) 0 = int 101 1 = int 2 = int 3 = int 4 = int 5
Array ([0] = [1] = [2] = [3] = [4] = [5] = 99)
Array ([0] = [1] = [2] = [3] = [4] = 104 [5] = 105)
========================================================================================
I'm a split line.
========================================================================================
The return value of a different array_walk () than Array_map () is a Boolean, meaning that if you want to modify the data of an array, you should callback function to tamper with (that is, reference)
Array_walk () passes the value of the element of the array, along with the key value, to the callback function, and also allows the data of its type to be passed to the callback function.
The following are the prototypes and official documentation:
BOOL array_walk (array &array, callback funcname [, mixed UserData])
Returns TRUEif successful, and FALSEif it fails.
Applies a user-defined function funcname to array each cell in the array. A typical case is funcname two parameters accepted. arraythe value of the parameter as the first, the key name as the second. If an optional parameter is provided userdata , it is passed as the third parameter to callback funcname .
If the funcname function requires more arguments than is given, a e_warning-level error is generated every time Array_walk () is called funcname . These warnings can be suppressed by adding the PHP error operator @ before the array_walk () call, or by using error_reporting ().
Note: If you funcname need to directly act on the values in the array, the funcname first parameter given is specified as a reference. Any changes to these cells will also change the original array itself.
Note: passing the key name and UserData to funcname the medium is a new addition to PHP 4.0.
Array_walk () is not array affected by internal array pointers. Array_walk () traverses the entire array regardless of the position of the pointer.
The user should not change the array itself in the callback function. For example, add/Remove units, unset units, and so on. If the array of Array_walk () is changed, the behavior of this function is undefined and unpredictable.
Here is a simple example that I wrote myself:
<?phpfunction Alter (& $value, $key, $userdata) {//Do not make any changes to the value of the element here, if you want to modify it, you need to add a reference to the Echo $key like me. $userdata. $value .‘ <br/> ';} $arr = Array ("A" = "a", "B" = "B", "C" = "C", "D" = "D");//array_walk--Apply user function/function prototypes to each member of the array: bool Array_walk (Array &array, callback funcname [, mixed UserData])//The first parameter is an array//the second parameter is the callback function name,//The third parameter is an optional parameter (will be passed to the callback function) ==============================================================================//array_map () will pass the callback function's// The first parameter is the value of the element and, if necessary, the & operator//second parameter is the key value, and if it needs to be modified, it is treated as above//the third parameter is the data that the user passes to the function (that is, the third parameter in Array_walk ()) Array_walk ($arr, ' Alter ', ". is.");? >
Output
A.is.a
b.is.b
C.is.c
D.is.d
----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
This article is from the "Debug Me" blog, so be sure to keep this source http://zaitebie.blog.51cto.com/11179646/1741956
PHP: Array manipulation functions Array_walk () and Array_map ()