Array Array_map (callable $callback, array $arr 1 [, Array $ ...])
Array_map () returns an array that contains the cells of all cells in the arr1 after the callback function.
The number of arguments accepted by callback should be consistent with the number of arrays passed to the Array_map () function.
| The code is as follows |
Copy Code |
|
function Fun ($n)
{
return $n * $n * $n;
}
$a = Array (1, 2, 3, 4, 5);
$b = Array_map (' fun ', $a); /* Each array unit to do three-time operation, return the array * *
Print_r ($b);
/*
Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)
*/
Array_map (' unlink ', glob (' *.txt '));/* Glob returns an array of "filename. txt" and deletes each file.
Array_map (' unlink ', glob (' *.* '));
Array_map (' unlink ', glob (' * ')); |
If you do not use Array_map (), each unit of an array can be manipulated and then properly assembled.
Let's look at an example.
| code is as follows |
copy code |
|
//single Array Use example
$websites =array (" G "=>" Google "," B "=>" Baidu "," Y "=> "Yahoo");
//Output original array
echo "<pre>";
Print_r ($websites);
echo "</pre>";
//define callback functions to be processed on a single array
function Change_value ($value) {
return Ucfirst ($value). ". COM ";
}
$urls =array_map (' Change_value ', $websites);
Echo <pre>;
Print_r ($urls);
Echo </pre > ";
//Examples of multiple arrays
$arr 1=array (1,3,5,7);
$arr 2=array (2,4,6,8)
//define callback functions that handle multiple arrays
function func1 ($a, $b) {
Return $a * $b;
}
$results =array_map (' func1 ', $arr 1, $arr 2);
echo "After processing multiple arrays with a callback function, the result returned:<br>";
Echo <pre > ";
Print_r ($results);
echo "</pre>"; |
There are friends who do not know the difference between Array_walk and Array_map, the following I briefly introduce
The map is primarily designed to get the new array of your callback functions, which is the result.
The main walk is to use your callback function once for each parameter, which is the process of processing.