I have an array where I'm testing for duplicate values. I want to get an array of only the duplicate values, to give an error message to the user, noting which are the offending values. I tried
$duplicates = array_diff( $array_with_dupes, array_unique($array_with_dupes) );
But that didn't return the only the duplicate values -- instead I got an empty array.
What's a simple way to do this?
$arr = array('a','a','b','c','d','d','e');
$arr_unique = array_unique($arr);
$arr_duplicates = array_diff_assoc($arr, $arr_unique);
print_r($arr_duplicates);
The above will return
Array
(
[1] => a
[5] => d
)
An answer is here ( Use array_diff_assoc
instead of array_diff
):
array_unique( array_diff_assoc( $array, array_unique( $array ) ) );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With