user1125394
user1125394

Reputation:

Php: remove (different cases) duplicates from array

probably an already discussed topic, but in Php I did not found an answer Is there a simpler way to realize in what follows:

$a = array("hello","hello","Hello","world","worlD");
$p=array();
foreach( $a as $v ){
    $p[strtolower($v)] = "";
}
print_r($p);

keep one single element, in small-case, for the array

Upvotes: 0

Views: 75

Answers (2)

Gumbo
Gumbo

Reputation: 655219

You can use array_flip to swap keys and values:

$a = array_flip(array_map('strtolower', $a));

Upvotes: 0

Yoshi
Yoshi

Reputation: 54649

something like:

$p = array_unique(array_map('strtolower', $a));

Upvotes: 3

Related Questions