PHP case-insensitive in_array function

前端 未结 11 609
故里飘歌
故里飘歌 2020-11-29 19:52

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

So with a source array like this:

$a= array(
 \'one\'         


        
11条回答
  •  孤独总比滥情好
    2020-11-29 20:36

    I wrote a simple function to check for a insensitive value in an array the code is below.

    function:

    function in_array_insensitive($needle, $haystack) {
       $needle = strtolower($needle);
       foreach($haystack as $k => $v) {
          $haystack[$k] = strtolower($v);
       }
       return in_array($needle, $haystack);
    }
    

    how to use:

    $array = array('one', 'two', 'three', 'four');
    var_dump(in_array_insensitive('fOUr', $array));
    

提交回复
热议问题