In this introduction of a PHP language in the more commonly used array operation function, Array_map function, when I first contact with this function, the function of the powerful to deter, but also more and more feel the fun of the PHP language.
Description
The best place to learn about PHP functions is the PHP manual, a description of this function in the PHP manual:
Array Array_map (callable $callback, array $arr 1 [, Array $ ...]
Array_map () returns an array that contains all the cells in the ARR1 after the callback action. The number of arguments callback accepts should be the same as the number of arrays passed to the Array_map () function. This means that the value of each cell of the $ARR1 array is stored in a new array as the value of the corresponding cell of the new array through the callback function, and there is a strong point where the function can pass in multiple arrays, that is, you can simultaneously manipulate the values of each cell of multiple arrays at the same time in your defined callback function
Operation Example
Example 1:<? PHP function Cube ($n) { return $n * $n;} $num = Array (1,2,3,4,5); Var_dump (Array_map ("Cube", $num)); Output: Array (5) {[0]=> int (1) [1]=> int (4) [2]=> int (9) [3]=> int (+) [4]=> int (25)}
Example 2 (anonymous function also valid): <? PHP $func = function ($n, $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]=& Gt String (55) "The value of the 1th position of this array is integer: 1" [1]=> string (55) "The value of the 2nd position of this array is integer: 2" [2]=> string (55) "The value of the 3rd position of this array is integer: 3" [3 ]=> string (55) "The value of the 4th position of this array is integer: 4" [4]=> string (55) "The value of the 5th position of this array is integer: 5"}
Note that the statement in the anonymous function in the second example must use double quotation marks, because it contains $n, $m two parameters, and in PHP, the characters inside the double quotation marks are parsed by the parser again, so that the values represented by the arguments are parsed.
Finally is engaged in data development often encounter problems, I am in the Laravel Framework for PHP development, in the use of data model in the data when the situation occurs, the data is returned to the object type (objclass), at this time is unable to directly use the key to take out a certain data, At this point, the general approach is to use the Json_decode () method to set its argument to true to return the array type of data, but at this point the returned array is a two-level array, like
$arr = Array (0 = = Array ( ' id ' = = 11111 ), 1 = = Array ( ' id ' = = 22222 ), 2 = AR Ray ( ' id ' = 33333 ));
You can now remove the ID with foreach, 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, the foreach traversal array is also very fast, both of which look at the personal will