This article mainly introduces how PHP applies callback functions to a given array Unit. It is a very important application. For more information, see
This article mainly introduces how PHP applies callback functions to a given array Unit. It is a very important application. For more information, see
Arrays are an important part of PHP programming. This article introduces the usage of the Array Function array_map () in PHP to implement the function of the callback to the given array unit. The details are as follows:
Array array_map (callable $ callback, array $ arr1 [, array $...])
Array_map () returns an array containing all the elements in arr1 that have passed the callback function.
The number of parameters accepted by callback should be the same as the number of arrays passed to the array_map () function.
The example program is as follows:
Function fun ($ n) {return $ n * $ n;} $ a = array (1, 2, 3, 4, 5 ); $ B = array_map ('fun ', $ a);/* Each array Unit performs a cubic operation and returns an array */print_r ($ B );
Output result:
Array ([0] = & gt; 1 [1] = & gt; 8 [2] = & gt; 27 [3] = & gt; 64 [4] = & gt; 125)
In addition, the array_map () function has the following usage:
Array_map ('unlink', glob ('*. txt ');/* glob returns an array composed of "file name .txt", and then deletes each file */array_map ('unlink', glob ('*. * '); array_map ('unlink', glob ('*'));
If array_map () is not used, operations on each cell in the array can only be traversed and assembled as appropriate.
More application readers can explore specific project requirements.