PHP array_unique() Function

What is PHP array_unique() Function?

If you want to remove duplicate values from an array and make it unique, use array_unique() function. The function doesn’t modify the original array, rather it returns a new array with-

  • keeping the first occurrence of each unique value and
  • preserving the original array’s key. So, the resulting array may not have sequential numeric keys (in case of an indexed array).

Syntax:

array_unique(array, sorting_flag)

Parameters:

The Function has 1 required parameter and 1 optional parameter-

array (Required): It specifies the input array.

sorting_flag (Optional): It specifies how the elements are compared for uniqueness. It has 4 types of comparison flags-

  • SORT_REGULAR – it indicates that all the elements will be treated as they are not changing their data types. It is the default flag meaning if you don’t use the 2nd parameter, the function uses this flag as the second parameter. Check example 2.
  • SORT_NUMERIC – it indicates that all the elements will be treated as numeric for comparison. When you want to compare with integers and numeric strings, use this sorting flag. In this comparison 100 and “100” are considered same element. Check example 3.
  • SORT_STRING – it indicates that all the elements will be treated as string for comparison. In this comparison “100” and 100 are considered same element. Check example 4. But, “PHP” and “php” are not same. Because the comparison is case-sensitive. Check example 5.
  • SORT_LOCALE_LOCALE – it indicates that all the elements will be compared as string based on locale settings. Before using this flag, you need to set the locate using setlocale() function. This function is useful when you want to compare strings of different lamguages. Check example 6.

Return Values:

The function returns the filtered array after removing the duplicate values and it doesn’t modify the original/input array.

Examples:

Example 1:

<pre>
<?php
$language = ["PHP", "HTML", "PHP", "CSS"];
$unique_language = array_unique($language);
print_r($unique_language);
?>
</pre>

Output:

Array
(
[0] => PHP
[1] => HTML
[3] => CSS
)

Example 2:

<pre>
<?php
$language = ["8.5", 11, 8.5, 27];
$unique_language = array_unique($language, SORT_REGULAR);
print_r($unique_language);
?>
</pre>

Output:

Array
(
[0] => 8.5
[1] => 11
[3] => 27
)

Example 3:

<pre>
<?php
$language = ["100", 101, 100, "101", 100];
$unique_language = array_unique($language, SORT_NUMERIC);
var_dump($unique_language);
?>
</pre>

Output:

array(2) {
[0]=>
string(3) "100"
[1]=>
int(101)
}

Example 4:

<pre>
<?php
$language = ["100", 101, 100, "101"];
$unique_language = array_unique($language, SORT_STRING);
var_dump($unique_language);
?>
</pre>

Output:

array(2) {
[0]=>
string(3) "100"
[1]=>
int(101)
}

Example 5:

<pre>
<?php
$language = ["PHP", "php", 100, "101"];
$unique_language = array_unique($language, SORT_STRING);
var_dump($unique_language);
?>
</pre>

Output:

array(4) {
[0]=>
string(3) "PHP"
[1]=>
string(3) "php"
[2]=>
int(100)
[3]=>
string(3) "101"
}

Example 6:

<pre>
<?php
setlocale(LC_ALL, 'fr_FR.utf8', 'fra');
$values = array("Cliché", "Voilà", "Bonjour", "Cliché", "Voilà");
$unique_values = array_unique($values, SORT_LOCALE_STRING);
print_r($unique_values);
?>
</pre>

Output:

Array
(
[0] => Cliché
[1] => Voilà
[2] => Bonjour
)

Notes:

The array_unique() function doesn’t work on multidimensional array.

PHP Version Support:

aaa

Summary: PHP array_unique() Function

The array_unique() function is a built-in PHP function and part of the PHP’s array functions. It is a very useful way to make an array unique.

Reference:

https://www.php.net/manual/en/function.array-unique.php