I found a few solutions but I can't decide which one to use. What is the most compact and effective solution to use php's array_unique()
function on a case-insensitive array?
Example:
$input = array('green', 'Green', 'blue', 'yellow', 'blue');
$result = array_unique($input);
print_r($result);
Result:
Array ( [0] => green [1] => Green [2] => blue [3] => yellow )
How do we remove the duplicate green
? As far as which one to remove, we assume that duplicates with uppercase characters are correct.
e.g. keep PHP
remove php
or keep PHP
remove Php
as PHP
has more uppercase characters.
So the result will be
Array ( [0] => Green [1] => blue [2] => yellow )
Notice that the Green with uppercase has been preserved.
Would this work?
$r = array_intersect_key($input, array_unique(array_map('strtolower', $input)));
Doesn't care about the specific case to keep but does the job, you can also try to call asort($input);
before the intersect to keep the capitalized values instead (demo at IDEOne.com).
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