Current Location: Home> Function Categories> array_unique

array_unique

Remove duplicate values ​​from the array
Name:array_unique
Category:Array
Programming Language:php
One-line Description:Delete duplicate values ​​in the array.

Definition and usage

array_unique() function removes duplicate values ​​in the array and returns the result array.

When the values ​​of several array elements are equal, only the first element is retained and the other elements are deleted.

The returned array has the key name.

Note: The preserved array will retain the keyname type of the first array item.

Example

Remove duplicate values ​​from the array:

 <?php
$a = array ( "a" => "red" , "b" => "green" , "c" => "red" ) ;
print_r ( array_unique ( $a ) ) ;
?>

Try it yourself

grammar

 array_unique ( array )
parameter describe
array Required. Specify array.
sortingtype

Optional. Specifies how to compare array elements/items. Possible values:

  • SORT_STRING - Default. Compare items as strings.
  • SORT_REGULAR - Order each item in a regular order (Standard ASCII, without changing the type).
  • SORT_NUMERIC - Process each item as a number.
  • SORT_LOCALE_STRING - Handle each item as a string, based on the current locale setting (can be changed via setlocale()).

illustrate

array_unique() first sorts the values ​​as strings, then retains only the first encountered key name for each value, and then ignores all subsequent key names. This does not mean that the first occurrence of the key name of the same value in an unsorted array will be preserved.

Similar Functions
Popular Articles