2

Possible Duplicate:
PHP case-insensitive in_array function

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('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. Because it is case sensitive.

2
  • do you assume the array is all lowercase or can the array contain values in any case? Commented Aug 5, 2011 at 12:42
  • no The can contain any case values. Commented Aug 5, 2011 at 12:56

1 Answer 1

6

If you want to apply strtolower on each element of the array, use array_map:

in_array(strtolower('ONE'), array_map('strtolower', $a));

Sign up to request clarification or add additional context in comments.

1 Comment

Edited to works with any of this strings. @Manikandan: you not really need arary_map here if $a always consist of lowercased strings

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.