In this tutorial, you shall learn how to remove duplicate values in given array in PHP using array_unique() function, with the help of example programs.

PHP – Remove duplicate values in array

To remove duplicate values in given array in PHP, we can use array_unique() function.

array_unique() function takes an array as argument, and returns a new array with only unique values from the given original array.

The syntax to call array_unique() function to remove the duplicates in array $arr is

</>
Copy
array_unique($arr)

Note: The function does not modify the original array, but returns a new array with duplicates removed.

Example

In this example, we take an array arr with some string values. We remove any duplicate values in the array using array_unique() function.

PHP Program

</>
Copy
<?php
  $arr = ["apple", "banana", "apple", "cherry", "cherry"];
  $result = array_unique($arr);
  print_r($result);
?>

Output

PHP - Remove duplicate values in array

Conclusion

In this PHP Tutorial, we learned how to remove the duplicate values in given array using array_unique() function.