PHP array_unique() function

PHP array_uintersect_uassoc() function
PHP array_unshift() function

The array_unique function removes duplicate values from the given array and returns an array that contains only unique values.

If an array contains duplicate values, the array_unique function will keep the first occurrence of the duplicate element, and also, the key type of the first occurrence of that element is preserved in the result.

What is the syntax of the array_unique function in PHP?

array_unique(array, sorttype)
ParameterDescription
arrayThe array to remove duplicate values from – Required
sorttypeOptional. Specifies how to compare the array elements/items. SORT_STRING (default) – Compare elements as strings
SORT_REGULAR – Compares normally without changing the type
SORT_NUMERIC – Compare as numeric values
SORT_LOCALE_STRING – Compare as a string according to the current locale
array_unique function in PHP

Example of array_unique function

Example 1. Remove duplicate values from the array.

<?php
$arr=array("a"=>"R","b"=>"G","c"=>"R");
print_r(array_unique($arr));
?>

PHP array_uintersect_uassoc() function
PHP array_unshift() function
en English
X
Scroll to Top