mapKey()
The mapKey() method all similar to map(), but this applies to keys
Example.php
<?php use kim\present\utils\arrays\ArrayUtils;
$arrayUtils = ArrayUtils::from(range(1,5));
//Squared all elements
$arrayUtils->mapKey(function($num){ return "$num * $num"; });
// expected output: [
// "1 * 1" => 1,
// "2 * 2" => 2,
// "3 * 3" => 3,
// "4 * 4" => 4,
// "5 * 5" => 5
//]
$arrayUtils->mapKey(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->mapKeyAs(callable $callback) : array;
ArrayUtils::mapKeyFrom(iterable $from, callable $callback) : ArrayUtils;
ArrayUtils::mapKeyFromAs(iterable $from, callable $callback) : array;
Last modified 2yr ago