Skip to content

Core\Arr::flip

Turns an array inside out: every value becomes a key, and every key becomes the value under it.

The values have to be whole numbers or strings, because those are the only things an array can use as a key. Every key of the result is a string, so a value that was the number 7 becomes the key "7".

Two entries that share a value can only give you one entry, because an array holds one value per key. The shared key keeps the place of the first of those entries, and its value is the key of the last of them. An array with no entries gives you an array with no entries.

Parameters

  • $aarray<int|uint|string>The array to flip. Every value must be an int, a uint or a string. 1 and "1" become the same key.

Return value

Returns array<string>. The old keys as string values, stored under the old values as keys, in the order each value first appears. When a value repeats, its entry stays where the value first appeared, and it holds the last key that had the value.

Changelog

VersionChange
0.0.6A 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

Look up by the value

<?nvs
// Shows how to read an entry by its value instead of by its key.
array<string> $codes = ['red' => 'ff0000', 'green' => '00ff00'];
// `Core\Arr::flip` makes every value a key and every key the value under it.
// This prints {"ff0000":"red","00ff00":"green"}.
array<string> $names = Core\Arr::flip($codes);
echo Core\Json::encode($names), "\n";
// You can now read the name of a colour by its code. This prints "green".
echo $names['00ff00'], "\n";
Output
{"ff0000":"red","00ff00":"green"}
green

Keys and shared values

<?nvs
// Shows what the keys of the result look like, and what happens when two
// entries share a value.
array<int> $positions = ['gold' => 1, 'silver' => 2, 'bronze' => 3];
// Every key of the result is a string, so the number 1 becomes the key "1".
// This prints {"1":"gold","2":"silver","3":"bronze"}.
echo Core\Json::encode(Core\Arr::flip($positions)), "\n";
array<string> $roleOf = ['ada' => 'admin', 'bob' => 'guest', 'cleo' => 'admin'];
// Two entries with the same value give you one entry. This prints
// {"admin":"cleo","guest":"bob"}.
echo Core\Json::encode(Core\Arr::flip($roleOf)), "\n";
Output
{"1":"gold","2":"silver","3":"bronze"}
{"admin":"cleo","guest":"bob"}

A list you can test against

<?nvs
// Shows how to turn a list of allowed values into keys. Reading a key is
// faster than searching the whole list for every value you check.
array<string> $allowed = ['csv', 'json', 'xml'];
// The values become the keys, and the values of the result are the positions
// the list held them at.
array<string> $isAllowed = Core\Arr::flip($allowed);
array<string> $wanted = ['json', 'exe'];
foreach ($wanted as string $format) {
// This prints "json is allowed" and then "exe is not allowed".
if (Core\Arr::hasKey($isAllowed, $format)) {
echo $format, ' is allowed', "\n";
} else {
echo $format, ' is not allowed', "\n";
}
}
Output
json is allowed
exe is not allowed