Definitions and usage
The Array_map () function returns an array of the actions of the user's custom function. The number of arguments accepted by the callback function should be the same as the number of arrays passed to the Array_map () function.
Grammar
Array_map (Function,array1,array2,array3 ...)
Parameters |
Description |
function |
Necessary. The name of the user's custom function, or null. |
Array1 |
Necessary. The specified array. |
Array2 |
Optional. The specified array. |
Array3 |
Optional. The specified array. |
Example 1
<?php
function MyFunction ($v) {
if ($v = = "Dog") {return
"Fido";
}
return $v;
}
$a = Array ("Horse", "Dog", "Cat");
Print_r (Array_map ("MyFunction", $a));
? >
Output:
Array ([0] => horse [1] => Fido [2] => Cat)
Example 2
Use multiple parameters:
<?php
function MyFunction ($v 1, $v 2) {
if ($v 1 = = $v 2) {return
"Same";
}
return "different";
}
$a 1 = array ("Horse", "Dog", "Cat");
$a 2 = Array ("Cow", "Dog", "Rat");
Print_r (Array_map ("MyFunction", $a 1, $a 2));
>
Output:
Array ([0] => different [1] => same [2] => different)
Example 3
See what happens when a custom function name is set to null:
<?php
$a 1 = array ("Dog", "Cat");
$a 2 = Array ("Puppy", "Kitten");
Print_r (Array_map (null, $a 1, $a 2));
? >
Output:
Array (
[0] => Array ([0] => Dog [1] => Puppy)
[1] => Array ([0] => Cat [1] => kitten)
)
Articles that you may be interested in
- PHP Array function array_walk () notes
- PHP generates a continuous number (letter) Array function range () analysis, PHP lottery program function
- PHP presses the element to the array header (Array_unshift usage)
- Use PHP functions in Smarty Templates and how to use multiple functions for a variable in smarty templates
- The difference between the PHP merge array + and the Array_merge
- Two-dimensional array removal of duplicate values and Array_unique functions
- Summary of PHP array functions
- How do I remove an element from a PHP array (unset,array_splice)?