In this tutorial we will learn How to Remove Duplicate Values from an array with PHP, For this we can use PHP array_unique() function, a built-in function made to delete or remove the duplicate values from any array.
Table of Contents
PHP array_unique() function
PHP array_unique() function is used to remove duplicate values from the array by keeping the first instance of repeated or duplicate values present in the array.
This means that if the same value is repeated twice or more in an array, the first value will replace all other values and the new unique array will keep it's key as well.
<?php
$array=array("One"=>"HTML","Two"=>"CSS","Three"=>"PHP","Four"=>"PHP");
echo "Array with Duplicate Values:" . "<br>";
print_r($array);
echo "<br>";
$uniqueArray = array_unique($array);
echo "Array with Unique Values:" . "<br>";
print_r($uniqueArray);
?>
In above example, duplicate value is removed from an array and both arrays are printed to see the difference.