Check and Remove Duplicate Values from PHP Array
Jun 17th, 2007
I want to check whether my array holds duplicate entry and if it holds duplicate values then remove those duplicate value.
This code will directly remove the duplicate value from an Array.
1. Remove duplicate values from an array
<?php $arrUnique = array_unique ($array1); ?>
This array_unique function work in this manner-
(String)element1 = (string)element2;
String representation of each value will be checked.
2. Check whether an array holds duplicate values
<?php
if (count(array_unique($arr3)) < count($arr3))
echo 'Duplicate entry in array';
else
echo 'No Duplicate values in array';
?>
So, after combining both codes, I can check and be assured that I have unique values in my array.
Related: PHP array - Common functions
Possibly Related posts:
