I'm looking for a way to make array_unique case-insensitive, so, for example, it would turn array('Rock', 'rock', 'Pop') into array('Rock', 'Pop'). I don't particularly mind which of the differently-cased variations makes it into the final array, just so long as there aren't any case-based duplicates. Any ideas?

Thanks,
//BHolley

    My idea would be to make all array values lowercase (array_map) before using array_unique.

      Or you could do this:

      function array_iunique($arr) 
      {
          foreach ($arr as $key => $value) {
              $arr[$key] = strtolower($value); // Or uc_first(), etc.
          }
          return array_unique($arr);
      }
      
      $new_array = array_iunique($array);

        Okay, I figured something out. I didn't want to auto-lowercase everyting, since its kinda dumb to do that if they're all uppercase or whatever.

        Here's the function (part of the GPL project muswap):

        function array_iunique($array) {
        
        /* Sort the array so it's alphabetical and so items beginning in uppercase (like OCRemix, which are usually more precise unless the user goes nuts with capslock) over lowercase ones (ocremix) */
        usort($array, 'strcmp');
        
        /* We make a reference array that is truly unique (but all lower case) to compare against */
        
        $finalArray = array(); /* Declare it as blank first, just in case) */
        $referenceArray = array();
        
        /* Every value has its lower-case equivalent compared to the reference array. If it's not there, it gets added in lowercase form to the reference array and in regular form to the final array. */
        foreach($array as $item) {
        
        if(!in_array(strtolower($item), $referenceArray)) {
        	$finalArray[] = $item;
        	$referenceArray[] = strtolower($item);
        	}
        }
        
        
        return $finalArray;
        
        }
          Write a Reply...