arrayutils
  • Home
  • 📖How to use?
    • âš¡Installation
  • 📖Methods
    • âš¡Static method
      • static from()
      • static of()
      • static mapToArray()
    • âš¡Generic method
      • join()
      • every()
      • some()
      • reduce()
        • reduceRight()
      • sum()
      • pop()
      • shift()
      • includes()
      • keyExists()
      • indexOf()
      • find()
        • findIndex()
      • first()
        • keyFirst()
      • last()
        • keyLast()
      • random()
        • keyRandom()
      • splice()
    • âš¡Chain method
      • chunk()
      • column()
      • combine()
      • concat()
        • concatSoft()
      • countValues()
      • diff()
        • diffAssoc()
        • diffKey()
      • fill()
        • fillKeys()
      • filter()
      • flat()
        • flatMap()
      • flip()
      • forEach()
      • intersect()
        • intersectAssoc()
        • intersectKey()
      • keys()
      • map()
        • mapAssoc()
        • mapKey()
      • pad()
      • push()
      • replace()
      • reverse()
      • slice()
      • sort()
        • sortKey()
      • unique()
      • unshift()
      • values()
  • 📖Suffixes
    • âš¡Suffix - From
    • âš¡Suffix - As
  • links
    • 📌Github repo
    • 📌Packagist project
    • 📌Poggit project
Powered by GitBook
On this page
  • Syntax
  • Parameter
  • Return value
  • Prefixing
  • References
  1. 📖Methods
  2. ⚡Chain method
  3. map()

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
//]

Syntax

$arrayUtils->mapKey(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->mapKeyAs(callable $callback) : array;
ArrayUtils::mapKeyFrom(iterable $from, callable $callback) : ArrayUtils;
ArrayUtils::mapKeyFromAs(iterable $from, callable $callback) : array;

References

PreviousmapAssoc()Nextpad()

Last updated 4 years ago

map()