Case-insensitive array search
PhpArraysPhp Problem Overview
I have an array like this:
$array = Array ( 0 => 'oooo',
1 => 'no',
2 => 'mmmm',
3 => 'yes' );
I'd like to search for a word "yes". I know about array_search()
, but I'd like to match "yes", "Yes" and "YES" as well.
How can I do this?
Php Solutions
Solution 1 - Php
array_search(strtolower($search), array_map('strtolower', $array));
Solution 2 - Php
You can use in_array()
instead of array_search()
.
$response = in_array('yes', array_map('strtolower', $array));
Solution 3 - Php
Edit: Sorry, I see it's for values, see: http://php.net/manual/en/function.array-change-key-case.php#88648
For keys:
$a = array('YES', 'yes', 'Yes');
$b = array_change_key_case($a, CASE_LOWER);
$f = array_search(strtolower($search), $b);