array_unique
array_unique
Removes duplicate values from an array
array **array_unique** array $array int $flags
Takes an input and returns a new array
without duplicate values.
array
Note that keys are preserved. If multiple elements compare equal under
the given , then the key and value of
the first equal element will be retained.
flags
Opmerking: > Two elements are considered equal if and only if , i.e. when the string representation is the same, the first element will be used.
(string) $elem1 === (string) $elem2
arrayThe input array.
flags
The optional second parameter
may be used to modify the comparison behavior using these values:
flags
Comparison type flags:
- - compare items normally (don't change types)`SORT_REGULAR`
-
- compare items numerically
SORT_NUMERIC
- compare items numerically
-
- compare items as strings
SORT_STRING
- compare items as strings
-
- compare items as strings, based on the current locale.
SORT_LOCALE_STRING
- compare items as strings, based on the current locale.
Returns the filtered array.
Voorbeeld: example
<?php
$input = ["a" => "green", "red", "b" => "green", "blue", "red"];
$result = array_unique($input);
print_r($result);
?>
Array
(
[a] => green
[0] => red
[1] => blue
)
Voorbeeld: and types
<?php
$input = [4, "4", "3", 4, 3, "3"];
$result = array_unique($input);
var_dump($result);
?>
array(2) {
[0] => int(4)
[2] => string(1) "3"
}
Opmerking: > Note that is not intended to work on multi dimensional arrays.
array_unique
array_count_values