Case-insensitive array search

PhpArrays

Php 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);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGowriView Question on Stackoverflow
Solution 1 - PhpHalil ÖzgürView Answer on Stackoverflow
Solution 2 - PhpbrunohdanielView Answer on Stackoverflow
Solution 3 - PhpAshleyView Answer on Stackoverflow