Skip to content
Advertisement

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('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.

Advertisement

Answer

you can use preg_grep():

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

print_r( preg_grep( "/ONe/i" , $a ) );
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement