php get duplicate values in array code example
Example 1: get duplicate value from array php
$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 )
Example 2: php get keys of duplicate values in array
<?php
$array = array('a', 'a', 'b', 'c', 'd');
$unique = array_unique($array);
$duplicates = array_diff_assoc($array, $unique);
$result = array_diff($unique, $duplicates);
$unique_keys = array_keys($result);
$duplicate_keys = array_keys(array_intersect($array, $duplicates));