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]

Syntax

$arrayUtils->map(callable $callback) : ArrayUtils;

Parameter

  • $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 array every was 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