1array_map ( callable $callback , array $array1 [, array $... ] ) : array
2
1
2 <?php
3function my_callback($item) {
4 return strlen($item);
5}
6
7
8 $strings = ["apple", "orange",
9 "banana", "coconut"];
10$lengths =
11 array_map("my_callback", $strings);
12print_r($lengths);
13?>
1The array_map() function sends each value of an array to a user-made function, and returns an array with new values, given by the user-made function.
2
3Tip: You can assign one array to the function, or as many as you like.
4
5Syntax
6array_map(functionname, array1, array2, array3, ...)
7
8Example
9Send each value of an array to a function, multiply each value by itself, and return an array with the new values:
10
11<?php
12function myfunction($val)
13{
14 return($val*$val);
15}
16
17$a=array(1,2,3,4,5);
18print_r(array_map("myfunction",$a));
19?>
1PHP function array_map(?callable $callback, array $array, array ...$arrays) array
2-----------------------------------------------------------------------------
3Applies the callback to the elements of the given arrays.
4
5Parameters:
6callable|null--$callback--Callback function to run for each element in each array.
7array--$array--An array to run through the callback function.
8array--...$arrays--[optional]
9
10Returns: an array containing all the elements of arr1 after applying the callback function to each one.