How to check duplicate records in an array using PHP

By | November 5, 2020
check duplicate records in an array using php

If you are looking for the code to check duplicate records in an array using PHP function, then you are in the right place.

In this post, I am going to create a function check_duplicates(), which will find the duplicate records in an array and will return the duplicate records as an array with the help of PHP in-built functions array_diff_key() and array_unique().

Here is the function:

function check_duplicates($arr)
{
  return array_diff_key($arr, array_unique($arr));
}

Call the above function to check duplicate records as below:

$arr = [10,12,13,10,10];
print_r(check_duplicates($arr));

Output:

Leave a Reply

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