PHP | array_unique() Function

Many times while writing programs or development we need to filter arrays to remove duplicates. PHP provides us with an inbuilt function to do this, making things easy for us. The array_unique() is a built-in function in PHP and this function removes duplicate values from an array. If there are multiple elements in the array with same values then the first appearing element will be kept and all other occurrences of this element will be removed from the array.

Also, according to this function two elements are considered equal if and only if (string) $elem1 === (string) $elem2 i.e. when the string representation of the elements is the same.

Syntax:



array array_unique($array , $sort_flags)

Note: The keys of array are preserved. That is, the keys of the not removed elements of the input array will be same in the output array.

Parameters: This function accepts two parameters out of which one is mandatory and the other is optional. Both of these parameters are described below:

  1. $array: This parameter is mandatory to be supplied and it specifies the input array from which we want to remove duplicates.
  2. $sort_flags: This is optional parameter. This parameter $sort_flags may be used to modify the sorting behavior using these values:
    • SORT_REGULAR: This is the default value of the parameter $sort_flags. This value tells the function to compare items normally (don’t change types).
    • SORT_NUMERIC: This value tells the function to compare items numerically.
    • SORT_STRING: This value tells the function to compare items as strings.
    • SORT_LOCALE_STRING: This value tells the function to compare items as strings, based on the current locale.

Return Value: The array_unique() function returns the filtered array after removing all duplicates from the array.

Below programs illustrate the array_unique() function in PHP:

Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Input Array
$a=array("red", "green", "red", "blue");
  
// Array after removing duplicates
print_r(array_unique($a));
  
?>

chevron_right


Output:

Array
(
    [0] => red
    [1] => green
    [3] => blue
)

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Input array
$arr = array("a"=>"MH", "b"=>"JK", "c"=>"JK", "d"=>"OR");
  
// Array after removing duplicates
print_r(array_unique($arr));
  
?>

chevron_right


Output:

Array
(
    [a] => MH
    [b] => JK
    [d] => OR
)

Important points to note:

  • The array_unique() is not intended to work on multi dimensional arrays.
  • The keys of the input array are preserved.
  • According to this function two elements are considered equal if their string representation is same.

Reference:
http:http://php.net/manual/en/function.array-unique.phpp



My Personal Notes arrow_drop_up

Sudipta sahana BTech 2nd year Cse student

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.