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

The optional second parameter flags may be used to modify the sorting behavior using these values:

ConstantDescription
SORT_REGULARcompare items normally (don't change types)
SORT_NUMERICcompare items numerically
SORT_STRINGcompare items as strings
SORT_LOCALE_STRINGcompare items as strings, based on the current locale

Return

Returns the filtered array.

Examples

1 · array

<?

$array = array("a" => "green", "red", "b" => "green", "blue", "red", "4", 4, "3", 3);

$return = array_unique($array);

print_r($return);

?>
Array
(
    [a] => green
    [0] => red
    [1] => blue
    [3] => 4
    [5] => 3
)

2 · flags · SORT_REGULAR

<?

$array = array("a" => "green", "red", "b" => "green", "blue", "red", "4", 4, "3", 3);
$flags = SORT_REGULAR;

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

print_r($return);

?>
Array
(
    [a] => green
    [0] => red
    [1] => blue
    [3] => 4
    [5] => 3
)

3 · flags · SORT_NUMERIC

<?

$array = array("a" => "green", "red", "b" => "green", "blue", "red", "4", 4, "3", 3);
$flags = SORT_NUMERIC;

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

print_r($return);

?>
Array
(
    [a] => green
    [3] => 4
    [5] => 3
)

4 · flags · SORT_STRING

<?

$array = array("a" => "green", "red", "b" => "green", "blue", "red", "4", 4, "3", 3);
$flags = SORT_STRING;

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

print_r($return);

?>
Array
(
    [a] => green
    [0] => red
    [1] => blue
    [3] => 4
    [5] => 3
)

5 · flags · SORT_LOCALE_STRING

<?

$category = LC_ALL;
$locale = "en_US";

setlocale($category, $locale);

$array = array("a" => "green", "red", "b" => "green", "blue", "red", "4", 4, "3", 3);
$flags = SORT_LOCALE_STRING;

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

print_r($return);

?>
Array
(
    [a] => green
    [0] => red
    [1] => blue
    [3] => 4
    [5] => 3
)
HomeMenu