Core\Arr::unique
Core\Arr::unique(array<T> $a, {by?: callable}): array<T>Core\Arr::unique( array<T> $a, {by?: callable}, ): array<T>
Gives you the entries of an array whose value has not appeared before.
The first entry with a value wins. It keeps its key and its position, and every later entry with the same value is dropped. The keys are not renumbered, so a list with repeated values comes back with gaps in its keys.
Two entries are the same when their values are identical: the same type and the same content, with no conversion. The number 1 and the text "1" are two different values, so both are kept.
The by option changes what is compared. It is a function that receives an entry and returns the value to compare it by. You still get back the whole entry, so you can keep one record per email address.
The examples show repeated values removed, upper and lower case ignored, and two lists of records merged without repeating anyone.
Parameters
$aarray<T>The array to deduplicate.
Options
bycallableoptionalCalled once per entry as($value, $key), and may declare fewer parameters. Its result is compared by identity, but the entry itself is what is kept.
Return value
Returns array<T>. The first occurrence of each value, under its own key and in its own position. For an array<E, T>, the result is an array<E, T> too.
Examples
Remove repeated values
<?nvs// Shows how to remove the values that appear more than once in a list.
array<string> $tags = ['news', 'sport', 'news', 'weather', 'sport'];
// The first entry with a value is kept, with the key it had. "news" keeps key// 0 and "weather" keeps key 3, so the keys have gaps in them.array<string> $once = Core\Arr::unique($tags);echo Core\Json::encode($once), "\n";{"0":"news","1":"sport","3":"weather"}Ignore upper and lower case
<?nvs// Shows how to remove repeated values while treating "Sport" and "sport" as// the same value. The `by` option says what to compare by.
array<string> $tags = ['News', 'sport', 'news', 'SPORT'];
// The entries are compared in lower case, so only "News" and "sport" are left.// You get the entries as they were written, not the lower case form.array<string> $once = Core\Arr::unique($tags, { by: fn(string $tag): string => Core\Str::lower($tag),});echo Core\Json::encode($once), "\n";["News","sport"]Merge two lists of people
<?nvs// Shows how to merge two lists of people without repeating anyone. The email// address says who a person is.
array<array<string>> $subscribers = [ ['email' => 'ada@example.com', 'name' => 'Ada Lovelace'], ['email' => 'grace@example.com', 'name' => 'Grace Hopper'],];array<array<string>> $customers = [ ['email' => 'ada@example.com', 'name' => 'A. Lovelace'], ['email' => 'alan@example.com', 'name' => 'Alan Turing'],];
// The first record for each address wins, so Ada keeps the name the// subscriber list gave her. Three people are left.array<array<string>> $everyone = Core\Arr::unique( Core\Arr::appendAll($subscribers, $customers), { by: fn(array<string> $person): string => $person['email'] },);echo Core\Json::encode($everyone), "\n";{"0":{"email":"ada@example.com","name":"Ada Lovelace"},"1":{"email":"grace@example.com","name":"Grace Hopper"},"3":{"email":"alan@example.com","name":"Alan Turing"}}