$arr = array(1, 4, 6, 1, 8, 9, 4, 6);
$unique = array_unique($arr);
$duplicates = array_diff_assoc($arr, $unique);
print_r($duplicates);
Array ( [3] => 1 [6] => 4 [7] => 6 )
$counts = array_count_values($array);
$duplicate_title = array_filter($array, function ($value) use ($counts) {
return $counts[$value] > 1;
});
<?php
/**
Examples of these functions:
array_unique,
array_diff_assoc,
array_diff,
array_keys,
array_intersect
Examle with an array:
*/
$array = array('a', 'a', 'b', 'c', 'd');
// Unique values
$unique = array_unique($array);
// Duplicates
$duplicates = array_diff_assoc($array, $unique);
// Unique values
$result = array_diff($unique, $duplicates);
// Get the unique keys
$unique_keys = array_keys($result);
// Get duplicate keys
$duplicate_keys = array_keys(array_intersect($array, $duplicates));
if (count($array) === count(array_unique($array))) {
//values are unique
}
function has_dupes($array) {
$dupe_array = array();
foreach ($array as $val) {
if (++$dupe_array[$val] > 1) {
return true;
}
}
return false;
}