williamg
williamg

Reputation: 2758

case-insensitive array_unique

I'm trying to write a few lines of code to make a case insensitive array unique type function. Here's what I have so far:

foreach ($topics as $value) {
    $lvalue = strtolower($value);
    $uvalue = strtolower($value);

    if (in_array($value, $topics) == FALSE || in_array($lvalue, $topics) == FALSE || in_array($uvalue, $topics) == FALSE) {
        array_push($utopics, $value);
    }
}

The trouble is the if statement. I think there's something wrong with my syntax, but I'm relatively new to PHP and I'm not sure what it is. Any help?

Upvotes: 39

Views: 17109

Answers (6)

aslawin
aslawin

Reputation: 1981

Another approach - use array_walk and create temporary table with values with lower case only as indexes, then return unique values. Example:

<?php

$values = ['test', 'Test', 'test1', 'test1', 'tEsT1', 'tesT2'];

function array_unique_insensitive( $array ) {
    $resultArray = [];
     array_walk($array, function (&$arrayItem) use (&$resultArray) {
        $key = strtolower($arrayItem);
        if (!isset($resultArray[$key])) {
            $resultArray[$key] = $arrayItem;
        }
    });
    
    return array_values(array_unique($resultArray));
}

var_dump(array_unique_insensitive($values));

The result of var_dump is:

array(3) {
  [0]=>
  string(4) "test"
  [1]=>
  string(5) "test1"
  [2]=>
  string(5) "tesT2"
}

Upvotes: 0

mickmackusa
mickmackusa

Reputation: 47883

Assuming:

  • you want to retain the first encountered value of any case-insensitive duplicate and
  • all values are strings and will not be mutated if used as keys

Simply populate a new array keyed by the multibyte-safe uppercase version of the value, then re-index after looping.

Code: (Demo)

$result = [];
foreach ($array as $v) {
    $result[mb_strtoupper($v)] ??= $v;
}
var_export(array_values($result));

Upvotes: 1

Pentium10
Pentium10

Reputation: 207863

function array_iunique( $array ) {
    return array_intersect_key(
        $array,
        array_unique( array_map( "strtolower", $array ) )
    );
}

Upvotes: 77

Drew G
Drew G

Reputation: 282

and another alternative...

function array_iunique($topics) {

    $ltopics = array_map('strtolower', $topics);
    $cleanedTopics = array_unique($ltopics);

    foreach($topics as $key => $value) {
        if(!isset($cleanedTopics[$key])) {
            unset($topics[$key]);
        }
    }

    return $topics;

}

Pentium10's is better though.

Upvotes: 0

user229044
user229044

Reputation: 239270

You're setting both lvalue and uvalue to the lower case version.

 $uvalue = strtolower($value);

should be

 $uvalue = strtoupper($value);

That said, this might be a little faster. The performance of your function will degrade exponentially, while this will be more or less linear (at a guess, not a comp-sci major...)

<?php

function array_iunique($ar) {
  $uniq = array();
  foreach ($ar as $value)
    $uniq[strtolower($value)] = $value;
  return array_values($uniq);
}
?>

Upvotes: 6

JordyOnrust
JordyOnrust

Reputation: 662

Should $uvalue not be uppercase? So

$uvalue = strtoupper($value):

Upvotes: 0

Related Questions