What is the use of array map in php?
(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8) Show array_map — Applies the callback to the elements of the given arrays Descriptionarray_map(?callable Parameterscallback A callable to run for each element in each array. array An array to run through the arrays Supplementary variable list of array arguments to run through the Return Values Returns an array containing the results of applying the The returned array will preserve the keys of the array argument if and only if exactly one array is passed. If more than one array is passed, the returned array will have sequential integer keys. Changelog
ExamplesExample #1 array_map() example
This makes $b have: Array ( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 ) Example #2 array_map() using a lambda function
Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 [4] => 10 ) Example #3 array_map() - using more arrays
map_Spanish(int $n, string $m): array The above example will output: // printout of $c Array ( [0] => The number 1 is called uno in Spanish [1] => The number 2 is called dos in Spanish [2] => The number 3 is called tres in Spanish [3] => The number 4 is called cuatro in Spanish [4] => The number 5 is called cinco in Spanish ) // printout of $d Array ( [0] => Array ( [1] => uno ) [1] => Array ( [2] => dos ) [2] => Array ( [3] => tres ) [3] => Array ( [4] => cuatro ) [4] => Array ( [5] => cinco ) ) Usually when using two or more arrays, they should be of equal length because the callback function is applied in parallel to the corresponding elements. If the arrays are of unequal length, shorter ones will be extended with empty elements to match the length of the longest. An interesting use of this function is to
construct an array of arrays, which can be easily performed by using Example #4 Performing a zip operation of arrays
The above example will output: Array ( [0] => Array ( [0] => 1 [1] => one [2] => uno ) [1] => Array ( [0] => 2 [1] => two [2] => dos ) [2] => Array ( [0] => 3 [1] => three [2] => tres ) [3] => Array ( [0] => 4 [1] => four [2] => cuatro ) [4] => Array ( [0] => 5 [1] => five [2] => cinco ) ) Example #5
The above example will output: array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } Example #6 array_map() - with string keys
The above example will output: array(1) { ["stringkey"]=> array(1) { [0]=> string(5) "value" } } array(1) { [0]=> array(2) { [0]=> string(5) "value" [1]=> string(5) "value" } } array(1) { ["stringkey"]=> string(5) "value" } array(1) { [0]=> array(2) { [0]=> string(5) "value" [1]=> string(5) "value" } } Example #7 array_map() - associative arrays While array_map() does not directly support using the array key as an input, that may be simulated using array_keys().
The above example will output: array(3) { [0]=> string(24) "v1 was the First release" [1]=> string(25) "v2 was the Second release" [2]=> string(24) "v3 was the Third release" } See Also
lukasz dot mordawski at gmail dot com ¶ 8 years ago
privateFilterMethod($value) { elfe1021 at gmail dot com ¶ 8 years ago
test($names) { radist-hack at yandex dot ru ¶ 13 years ago
Mahn ¶ 6 years ago
stijnleenknegt at gmail dot com ¶ 14 years ago
CertaiN ¶ 9 years ago
anonymous_user ¶ 9 months ago
Walf ¶ 5 months ago
What is a map in PHP?A Map is a sequential collection of key-value pairs, almost identical to an array used in a similar context. Keys can be any type, but must be unique. Values are replaced if added to the map using the same key.
Does array map preserve keys?The returned array will preserve the keys of the array argument if and only if exactly one array is passed. If more than one array is passed, the returned array will have sequential integer keys.
Why do we need array in PHP?Arrays in PHP are a type of data structure, which allows us to saves the efforts of creating a different variable in order to store multiple elements with a similar data type under a single variable. The arrays help to create a list of similar elements, accessible by index or key.
Is there a map function in PHP?PHP | Ds\Map map() Function
The Ds\Map::map() function of the Map class in PHP is used to apply a callback function to a Map object. This returns the result of applying the callback function to each value present on the map.
|