Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is it possible to do case-insensitive comparison when using the in_array function?

So with a source array like this:

$a= array(
 'one',
 'two',
 'three',
 'four'
);

The following lookups would all return true:

in_array('one', $a);
in_array('two', $a);
in_array('ONE', $a);
in_array('fOUr', $a);

What function or set of functions would do the same? I don't think in_array itself can do this.

share|improve this question
add comment

4 Answers

up vote 31 down vote accepted

you can use preg_grep():

$a= array(
 'one',
 'two',
 'three',
 'four'
);

print_r( preg_grep( "/ONe/i" , $a ) );
share|improve this answer
 
+1 good suggestion –  cletus Jan 30 '10 at 2:10
12  
using regular expressions isn't a good solution, because it can be slow... maybe array_map is faster –  Philipp Oppermann Feb 2 '12 at 15:05
2  
To make it a drop-in replacement for in_array, returning a bool, it becomes: count(preg_grep('/^'.preg_quote($needle).'/$',$a)>0). Not so elegant, then. (Notice the ^ and $ characters are required, unless partial matching is desired.) However if you actually want the matching entries returned, I like this solution. –  Darren Cook Sep 14 '12 at 6:57
1  
The last comment contains a syntax error: /$ should be $/ instead. –  gogowitsch Dec 12 '12 at 15:01
1  
@DarrenCook as far as i know a bool cast would also work (bool)preg_grep('/^'.preg_quote($needle).'$/',$a), as an empty array would cast to false –  arraintxo Apr 9 '13 at 12:20
show 2 more comments

The obvious thing to do is just convert the search term to lowercase:

if (in_array(strtolower($word), $a) { 
  ...

of course if there are uppercase letters in the array you'll need to do this first:

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

and search that. There's no point in doing strtolower on the whole array with every search.

Searching arrays however is linear. If you have a large array or you're going to do this a lot, it would be better to put the search terms in key of the array as this will be much faster access:

$search_array = array_combine(array_map('strtolower', $a), $a);

then

if ($search_array[strtolower($word)]) { 
  ...

The only issue here is that array keys must be unique so if you have a collision (eg "One" and "one") you will lose all but one.

share|improve this answer
add comment
function in_arrayi($needle, $haystack) {
    return in_array(strtolower($needle), array_map('strtolower', $haystack));
}

From Documenation

share|improve this answer
 
You should blockquote code (or anything really) you get from somewhere else. –  cletus Jan 30 '10 at 2:05
6  
I figured that the 'From Documentation' with a link would be sufficient... –  Chacha102 Jan 30 '10 at 2:05
 
Just to be clear. It's not a criticism. Just a suggestion (and only my opinion, nothing official). :) At least if I copy a code snippet from a page I'll block quote it. –  cletus Jan 30 '10 at 2:07
 
Plus, using a code block better describes it, as it is 'code'. Block quoting it does not allow for it to be properly formatted. –  Chacha102 Jan 30 '10 at 2:08
 
You can put a code block inside a block quote. –  cletus Jan 30 '10 at 2:08
show 4 more comments
function in_arrayi($needle, $haystack) {
    return in_array(strtolower($needle), array_map('strtolower', $haystack));
}

Source: php.net in_array manual page.

share|improve this answer
 
If you know what is in the array, you can leave out the array_map; but this is a good example. –  Don Jan 30 '10 at 1:58
1  
Who downvoted a perfectly good example and solution? –  Doug Neiner Jan 30 '10 at 2:00
 
I did actually. Because mapping the array on every call is, well, ludicrous. –  cletus Jan 30 '10 at 2:02
 
Also, assuming (like Chacha) this comes direct from the docs, it's better to block quote it. –  cletus Jan 30 '10 at 2:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.