The array_map function of PHP introduces an array operation function that is commonly used in the PHP language. the array_map function shocked the powerful functions of this function when I first came into contact with this function, more and more people feel the fun of the PHP language.
Description
The best place to learn about PHP functions is the PHP Manual, which describes the function:
Array array_map (callable $ callback, array $ arr1 [, array $...])
Array_map () returns an array that contains 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. That is to say, set the value of each unit in the $ arr1 array, after the callback function is used, a new array is stored as the value of the corresponding unit of the new array. However, this function can be used to input multiple arrays, that is, in the callback function you define, you can simultaneously operate the values of each cell in multiple arrays.
Operation example
Example 1: <? Php function cube ($ n) {return $ n * $ n;} $ num = array (1, 2, 4, 5); var_dump (array_map ("cube ", $ num); output: array (5) {[0] => int (1) [1] => int (4) [2] => int (9) [3] => int (16) [4] => int (25 )}
Example 2 (the anonymous function is also valid): <? Php $ func = function ($ n, $ m) {return ("The value of the $ n position of this array is $ m
") ;}; $ A = array (1, 2, 3, 4, 5); $ B = ['integer: 1', 'integer: 2', 'integer: 3', 'integer: 4', 'integer: 5']; var_dump (array_map ($ func, $ a, $ B); output: array (5) {[0] => string (55) "The 1st position value of this array is an integer: 1" [1] => string (55) "The value of the 2nd position in this array is an integer: 2" [2] => string (55) "The value of the 3rd position in this array is an integer: 3 "[3] => string (55)" The 4th position value of this array is an integer: 4 "[4] => string (55) "The value of the 5th position in this array is an integer: 5 "}
Note that the statement in the anonymous function in the second example must use double quotation marks, because it contains two parameters: $ n and $ m. in PHP, the characters in double quotation marks will be parsed by the analyzer once so that the values represented by the parameters can be parsed.
Finally, I often encounter problems when I am engaged in data development. I am developing PHP under the laravel framework. when I use the data model to retrieve data, there will be a situation, the retrieved data is the type of the returned object (ObjClass). In this case, you cannot directly use the key to retrieve a data. in this case, the json_decode () method is generally used, if its parameter is set to true, data of the array type can be returned, but the returned array is an array of two layers, just like
$arr = array( 0 => array( 'id' => 11111 ), 1 => array( 'id' => 22222 ), 2 => array( 'id' => 33333 ));
In this case, you can use foreach to retrieve the id, but you can also use the array_map () method.
$ Id_arr = array_map (function ($ v) {return $ v ['id'] ;}, $ arr); output: array (3) {[0] => int (111) [1] => int (222) [2] => int (333 )}
However, foreach can traverse the array quickly. The two methods are based on your wishes.