Php array function-array_map () function returns the array after the user-defined function acts. The number of parameters accepted by the callback function should be consistent with the number of arrays passed to the array_map () function. Array_map (function, array1, array2, array3. ..); function: required. User-defined function name, or nullarray1: required. Specify the array arra php array function-array_map ()
The array_map () function returns the array after the user-defined function is applied. Parameters accepted by the callback function
The number should be consistent with the number of arrays passed to the array_map () function.
Array_map (function, array1, array2, array3 ...);
Function: required. User-defined function name, or null
Array1: required. Specified array
Array2: Optional. Specified array
Array3: Optional. Specified array
Function myfunction ($ v ){
If ($ v = 'dog '){
Return 'fid ';
}
Return $ v;
}
$ A = array ('horse', 'dog', 'cat ');
Print_r (array_map ('myfunction', $ ));
?>
Output: Array ([0] => Horse [1] => Fido [2] => Cat)
Use multiple parameters:
Function myfunction ($ v1, $ v2 ){
If ($ v1 ===$ v2 ){
Return "same ";
} Else {
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)
When the custom function name is set to null:
$ 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 ))