map()
The map() method applies the callback to the elements
Example.php
<?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]
$arrayUtils->map(callable $callback) : ArrayUtils;
$callback
A function that produces an element of the new Array, taking three arguments:$value
The current element being processed in the array.$key
The index of the current element being processed in the array.$array
The arrayevery
was called upon.
- A new array with each element being the result of the callback function.
$arrayUtils->mapAs(callable $callback) : array;
ArrayUtils::mapFrom(iterable $from, callable $callback) : ArrayUtils;
ArrayUtils::mapFromAs(iterable $from, callable $callback) : array;
Last modified 2yr ago