0

Right now , I have a return function :

return array_unique(array_merge( $sizes, $custom_sizes ));

My problem is that a certain key can be lower case in one, and upper case in other .

For example , I can get "Thumbnails" in $sizes and "thumbnails" in $custom_sizes - at which case I would of course want to drop one .

(same case for names :

"starwars" vr. "StarWars" vr. "Starwars" vr. "STARWARS")

How can I make array_unique() be non case - sensitive ?

EDIT I : Following comments , a clarification :

I would also want to be able to CHOOSE which version to be kept (the one from the 1st array, or the one from the second..)

Obmerk Kronen
  • 15,619
  • 16
  • 66
  • 105
  • Possible duplicate of http://stackoverflow.com/questions/2276349/case-insensitive-array-unique – Yaniro Jun 03 '12 at 08:02
  • btw, you should not use `array_unique()` even if it would apply he usecase. The implementation of the function is such , that it sorts the values before filtering them (you can look it up in the PHP source). – tereško Jun 03 '12 at 12:31

1 Answers1

1

First hit on google is the PHP.net page which offers:

function in_iarray($str, $a){
    foreach($a as $v){
        if(strcasecmp($str, $v)==0){return true;}
    }
    return false;
}

function array_iunique($a){
    $n = array();
    foreach($a as $k=>$v){
        if(!in_iarray($v, $n)){$n[$k]=$v;}
    }
    return $n;
}

$input = array("aAa","bBb","cCc","AaA","ccC","ccc","CCC","bBB","AAA","XXX");
$result = array_iunique($input);
print_r($result);

/*
Array
(
    [0] => aAa
    [1] => bBb
    [2] => cCc
    [9] => XXX
)
*/
Paul
  • 8,974
  • 3
  • 28
  • 48
  • I saw this on php.net before - but I am sorry, I am a bit of a novice - how do I apply this to array_merge ? I need to pass this routine on both arrays before ? and also, how can I CHOOSE which version of "STARWARS" I want kept ? – Obmerk Kronen Jun 03 '12 at 08:12
  • First answer would be: `$result = array_iunique(array_merge($sizes, $custom_sizes));`. Second answer: you can't. Well, you can, but then there is no way to automate that in a way that saves some code. E.g. if you want to keep "STARWARS", your script must first get all instances of the word and has to check if "STARWARS" is actually present. Otherwise, you could loose all "StarWars", "starwars", and so on. – Paul Jun 03 '12 at 18:39
  • yes, in that case I know , but my question was not exactly on preferences to a single "word" (because, like you pointed out - sometimes I do not even KNOW the words" - my question was more how can I give preference to a certain ARRAY out of the two .. – Obmerk Kronen Jun 04 '12 at 01:09
  • You have to define in what way one value is superior to another (e.g. first letter capitalized) and add such a check to the set of functions. – Paul Jun 04 '12 at 07:32