map()
The map() method applies the callback to the elements
<?php use kim\present\utils\arrays\ArrayUtils;
$arrayUtils = ArrayUtils::from(range(1,10));
//Squared all elements
$arrayUtils->map(function($num){ return $num * $num; });
// expected output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]Syntax
$arrayUtils->map(callable $callback) : ArrayUtils;Parameter
$callbackA function that produces an element of the new Array, taking three arguments:
$valueThe current element being processed in the array.$keyThe index of the current element being processed in the array.$arrayThe arrayeverywas called upon.
Return value
A new array with each element being the result of the callback function.
Prefixing
$arrayUtils->mapAs(callable $callback) : array;ArrayUtils::mapFrom(iterable $from, callable $callback) : ArrayUtils;ArrayUtils::mapFromAs(iterable $from, callable $callback) : array;References
Last updated