Home > Article > Backend Development > php array_map uses a custom function to process each value in the array
array_map applies the callback function to the cells of the given array.
Description
array array_map ( callable $callback , array $arr1 [, array $... ] )
array_map() function converts user-defined functions Applies to each value in the array and returns the array with the new values 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.
Parameter introduction:
Return value
Returns an array, each element of the array is in the array (arr1) Each element is processed through a callback function.
Example:
<?php function cube ( $n ) { return( $n * $n * $n ); } $a = array( 1 , 2 , 3 , 4 , 5 ); $b = array_map ( "cube" , $a ); print_r ( $b ); ?>
Online operation
Output result:
Array ( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 )
Thank you for reading, I hope it can help you, thank you for your support of this site!
For more php array_map uses custom functions to process each value in the array, please pay attention to the PHP Chinese website!