I want to retrieve all duplicated entries from a array. Is this possible in PHP?
array(
1 => '1233',
2 => '12334',
3 => 'Hello',
4 => 'hello',
5 => 'U'
);
I want to return an array with just the duplicate value: “hello”.
Desired output array:
array(
1 => 'Hello',
2 => 'hello'
);
You will need to make your function case insensitive to get the "Hello" => "hello" result you are looking for, try this method:
Output is:
Edit by @AlixAxel:
This answer is very misleading. It only works in this specific condition. This counter-example:
Fails miserably. Also, this is not the way to keep duplicates:
Since one of the duplicated values will be in
array_unique
, and then chopped off byarray_diff
.Edit by @RyanDay:
So look at @Srikanth's or @Bucabay's answer, which work for all cases (look for case insensitive in Bucabay's), not just the test data specified in the question.