How can two arrays be compared in PHP to keep only the duplicate entries in a new array?

To compare two arrays and keep only the duplicate entries in a new array in PHP, you can use the array_intersect() function. This function compares the values of two arrays and returns the values that are present in both arrays. By using this function, you can easily create a new array containing only the duplicate entries from the original arrays.

$array1 = [1, 2, 3, 4, 5];
$array2 = [3, 4, 5, 6, 7];

$duplicateArray = array_intersect($array1, $array2);

print_r($duplicateArray);