Remove Duplicates From Array in PHP
In this article, we will discuss how to remove duplicates values from an array in php. For that, we need to use PHP array_unique() function. This function allows stripping an array of duplicate values with its array_unique() function, which accepts an array and returns a new array containing only unique values. So, If you are in a situation your array has only unique values then you need to use PHP array_unique function. Let’s see the example code:
Ex:
<?php
$duplicates = array('a', 'b', 'a', 'b', 'd', 'c','d');
$uniques = array_unique($duplicates); // remove duplicate values
print_r($uniques);
?>
Output:
a, b, d, c
- Here, we have created duplicate values in the array “duplicates”.
- Then we want to remove duplicate values, so we use the arrray_unique() function and pass the array “duplicate”.
- Then print the duplicate array. See the output, the duplicate values is removed.