Core\Arr::mapKeys
Makes a new array in which every value is stored under a key that a function returns for it. The values stay as they are, and only the keys change.
You give Core\Arr::mapKeys an array and a function. The function receives the value and the key, and returns the new key. A function that needs only the value declares only that one parameter. A new key is a whole number or a text.
The entries keep the order they had. When two entries get the same new key, they become one entry, and the last of them wins. A whole number and its text are the same key, so 1 and "1" name one entry.
Good to know: to change the values instead of the keys, use Core\Arr::map. To collect entries into groups under a shared key, use Core\Arr::groupBy.
Parameters
$aarray<T>The array whose keys change.$fncallable(T, string): int|uint|stringCalled as($value, $key)per entry, and may declare fewer parameters. It returns the entry's new key, anint, auintor astring.
Return value
Returns array<T>. The values under their new keys, in the order each key first appears. When two entries get the same key, the last one is kept. 1 and "1" are the same key.
Changelog
| Version | Change |
|---|---|
0.0.6 | A Core\Arr function that takes a key, such as hasKey, gave a compile error for a uint key (an unsigned whole number). It now takes a uint key, as $a[$key] already did. So Core\Arr::fillKeys($ids, true) works when $ids is a list of uint values. |
Examples
Write the keys differently
<?nvs// Stores every value under a new key. Here the header names are written in// lower case, so later lookups always find them.
array<string> $headers = ['Content-Type' => 'text/html', 'X-Request-Id' => 'a1b2'];
// The function receives the value and the key, and returns the new key.array<string> $lower = Core\Arr::mapKeys($headers, fn(string $value, string $name) => Core\Str::lower($name));
// This prints `content-type: text/html` and `x-request-id: a1b2`.foreach ($lower as string $name => string $value) { echo $name, ': ', $value, "\n";}content-type: text/htmlx-request-id: a1b2Build a lookup table
<?nvs// Turns a list of names into a lookup table. Each name becomes a key, so a// test for one name does not read the whole list.
array<string> $admins = ['ida', 'ben', 'sara', 'ben'];
// A function that needs only the value declares only that one parameter.array<string> $byName = Core\Arr::mapKeys($admins, fn(string $name) => $name);
// The list has four entries and the table has three. Two entries with the same// new key become one entry. This prints 3.echo Core\Arr::count($byName), "\n";
// This prints `yes` and then `no`.echo Core\Arr::hasKey($byName, 'ben') ? 'yes' : 'no', "\n";echo Core\Arr::hasKey($byName, 'max') ? 'yes' : 'no', "\n";3yesnoFind a record by its id
<?nvs// Stores a list of records under their own id. A program does this after it// reads rows from a database, so that it can find one row by its id.
class Customer{ public string $id; public string $city;
public function constructor(string $id, string $city) { $this->id = $id; $this->city = $city; }}
array<Customer> $rows = [ new Customer('c-17', 'Bonn'), new Customer('c-42', 'Linz'),];
array<Customer> $byId = Core\Arr::mapKeys($rows, fn(Customer $row) => $row->id);
// This prints `Linz`.echo $byId['c-42']->city, "\n";Linz