Home > Article > Backend Development > Does php have map?
Does php have a map?
php has a map, that is, the array_map() function applies user-defined functions to on each value in the array, and returns the array with the new value after the user-defined function is applied.
The number of parameters accepted by the callback function should be consistent with the number of arrays passed to the array_map() function.
Tip: You can input one or more arrays to the function.
Syntax
array_map(myfunction,array1,array2,array3...)
Parameters
myfunction is required. The name of the user-defined function, or null.
array1 Required. Specifies an array.
array2 Optional. Specifies an array.
array3 Optional. Specifies an array.
Return Value: Returns an array containing the values of array1, after applying the custom function to each value.
Example
Use user-defined function to change the value of the array:
<?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 )
More PHP related For knowledge, please visit PHP中文网!
The above is the detailed content of Does php have map?. For more information, please follow other related articles on the PHP Chinese website!