How to find out duplicate value in array PHP

How to find out duplicate value in array PHP

 

// Create an array in PHP
$data_array = array('laptop', 'mobile', 'computer', 'keyboard', 'laptop',
'mouse', 'printer', 'printer', 'printer');

// Now output is

print_r(array_count_values($data_array));

Array ( 
        [laptop] => 2
        [mobile] => 1 
        [computer] => 1 
        [keyboard] => 1 
        [mouse] => 1
        [printer] => 3 
)

// Then find the duplicate values

if(count(array_unique($data_array))<count($data_array))
{
    /* Arrray find out the duplate value here */
}
else
{
   /* Print array single value here */
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *