HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

array_unique

Description

The array_unique of Array for PHP removes duplicate values from an array.

Syntax

array_unique(
    array $array,
    int $flags = SORT_STRING
): array

Parameters

array

The input array.

flags

Modify the sorting behavior.

ValueConstantDescription
0SORT_REGULARcompare items normally (don't change types)
1SORT_NUMERICcompare items numerically
2SORT_STRINGcompare items as strings
5SORT_LOCALE_STRINGcompare items as strings, based on the current locale

Return

Returns the filtered array.

Examples

1 · array

<?

$array =
[
    null,
    false,
    "",
    10,
    10.0,
    1e1,
    "10",
    "10.0",
    "ß",
    "ss"
];

$return = array_unique($array);

print_r($return);
var_dump($return);
Array
(
    [0] => 
    [3] => 10
    [7] => 10.0
    [8] => ß
    [9] => ss
)
array(5) {
  [0]=>
  NULL
  [3]=>
  int(10)
  [7]=>
  string(4) "10.0"
  [8]=>
  string(2) "ß"
  [9]=>
  string(2) "ss"
}

2 · flags · SORT_REGULAR

<?

$array =
[
    null,
    false,
    "",
    10,
    10.0,
    1e1,
    "10",
    "10.0",
    "ß",
    "ss"
];
$flags = SORT_REGULAR;

$return = array_unique($array, $flags);

print_r($return);
var_dump($return);
Array
(
    [0] => 
    [3] => 10
    [8] => ß
    [9] => ss
)
array(4) {
  [0]=>
  NULL
  [3]=>
  int(10)
  [8]=>
  string(2) "ß"
  [9]=>
  string(2) "ss"
}

3 · flags · SORT_NUMERIC

<?

$array =
[
    null,
    false,
    "",
    10,
    10.0,
    1e1,
    "10",
    "10.0",
    "ß",
    "ss"
];
$flags = SORT_NUMERIC;

$return = array_unique($array, $flags);

print_r($return);
var_dump($return);
Array
(
    [0] => 
    [3] => 10
)
array(2) {
  [0]=>
  NULL
  [3]=>
  int(10)
}

4 · flags · SORT_STRING

<?

$array =
[
    null,
    false,
    "",
    10,
    10.0,
    1e1,
    "10",
    "10.0",
    "ß",
    "ss"
];
$flags = SORT_STRING;

$return = array_unique($array, $flags);

print_r($return);
var_dump($return);
Array
(
    [0] => 
    [3] => 10
    [7] => 10.0
    [8] => ß
    [9] => ss
)
array(5) {
  [0]=>
  NULL
  [3]=>
  int(10)
  [7]=>
  string(4) "10.0"
  [8]=>
  string(2) "ß"
  [9]=>
  string(2) "ss"
}

5 · flags · SORT_LOCALE_STRING

<?

$category = LC_ALL;
$locale_array =
[
    "de_DE@euro",
    "de_DE",
    "de",
    "ge"
];

setlocale($category, $locale_array);

$array =
[
    null,
    false,
    "",
    10,
    10.0,
    1e1,
    "10",
    "10.0",
    "ß",
    "ss"
];
$flags = SORT_LOCALE_STRING;

$return = array_unique($array, $flags);

print_r($return);
var_dump($return);
Array
(
    [0] => 
    [3] => 10
    [7] => 10.0
    [8] => ß
    [9] => ss
)
array(5) {
  [0]=>
  NULL
  [3]=>
  int(10)
  [7]=>
  string(4) "10.0"
  [8]=>
  string(2) "ß"
  [9]=>
  string(2) "ss"
}