How to detect duplicate values in PHP array

In PHP one dimensional array to find the duplicate values use array_count_values($Array)

For example, given the following array:

$Array = array(‘Neha’, ‘Komal’, ‘Nidhi’, ‘Niti’, ‘Komal’, ‘Nidhi’, ‘Gaurav’, ‘Kittu’, ‘Komal’);

I would like to print:

Neha => 1
Komal => 3
Nidhi => 2
Niti  => 1
Gaurav => 1
Kittu  => 1

Solution

For the below Array

$Array = array(‘Neha’, ‘Komal’, ‘Nidhi’, ‘Niti’, ‘Komal’, ‘Nidhi’, ‘Gaurav’, ‘Kittu’, ‘Komal’);

echo “<pre>”;
print_r(array_count_values($Array));
echo “</pre>”;

The Output will be

Neha => 1
Komal => 3
Nidhi => 2
Niti  => 1
Gaurav => 1
Kittu  => 1

Please follow and like us:
0

You may also like...

Leave a Reply

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

Time limit is exhausted. Please reload the CAPTCHA.