PHP Array Function array_map () Notes
Definition and usage
The array_map () function returns the array after the User-Defined Function is applied. The number of parameters accepted by the callback function should be the same as the number of arrays passed to the array_map () function.
Syntax
Array_map (function, array1, array2, array3 ...)
Parameters |
Description |
Function |
Required. The name of the User-Defined Function, or null. |
Array1 |
Required. Specified array. |
Array2 |
Optional. Specified array. |
Array3 |
Optional. Specified array. |
Example 1
<?phpfunction 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:
<?phpfunction myfunction($v1, $v2) {if ($v1 === $v2) {return "same";}return "different";}$a1 = array("Horse", "Dog", "Cat");$a2 = array("Cow", "Dog", "Rat");print_r(array_map("myfunction", $a1, $a2));?>
Output:
Array ([0] => different [1] => same [2] => different)
Example 3
See when the custom function name is set to null:
<?php$a1 = array("Dog", "Cat");$a2 = array("Puppy", "Kitten");print_r(array_map(null, $a1, $a2));?>
Output:
Array (
[0] => Array ([0] => Dog [1] => Puppy)
[1] => Array ([0] => Cat [1] => Kitten)
)
Articles you may be interested in
- PHP Array Function array_walk () Notes
- PHP generates continuous numbers (letters) Array Function range () analysis, PHP lottery program function
- Php pushes an element to the array header (array_unshift usage)
- Use php functions in the smarty template and how to use multiple functions for a variable in the smarty Template
- Php uses the filter function to verify the mailbox, url, and IP address
- PHP merges arrays + and array_merge
- Introduction to array union, intersection, and difference set functions in php
- Php searches for the existence of a value in the array (in_array (), array_search (), array_key_exists ())