Oihana PHP

unique.php

Table of Contents

Functions

unique()  : array<string|int, mixed>
Removes duplicate values from an array and reindexes it.

Functions

unique()

Removes duplicate values from an array and reindexes it.

unique(array<string|int, mixed> $array[, int $flags = SORT_STRING ]) : array<string|int, mixed>

Combines the functionality of array_unique() and array_values(): first removes duplicates, then reindexes the array numerically starting at 0.

The optional $flags parameter modifies the comparison behavior, possible values:

  • SORT_REGULAR: Compare items normally (don't change types).
  • SORT_NUMERIC: Compare items numerically.
  • SORT_STRING: Compare items as strings.
  • SORT_LOCALE_STRING: Compare items as strings, based on the current locale.
Parameters
$array : array<string|int, mixed>

The input array.

$flags : int = SORT_STRING

[optional] Comparison behavior flag. Default is SORT_STRING.

Tags
example
use function oihana\core\arrays\unique;

$array = ['a', 'b', 'a', 'c', 'b'];
$result = unique($array);
print_r($result); // ['a', 'b', 'c']

$numericArray = [1, 2, '1', 3];
$resultNumeric = unique($numericArray, SORT_NUMERIC);
print_r($resultNumeric); // [1, 2, 3]
link
https://www.php.net/manual/en/function.array-unique.php
link
https://www.php.net/manual/en/function.array-values.php
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
array<string|int, mixed>

The filtered array with unique values, reindexed from 0.


        
On this page

Search results