PHP array_ merge array_unique lower/upper case

1.5k Views Asked by At

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..)

1

There are 1 best solutions below

4
On BEST ANSWER

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
)
*/