PHP case-insensitive in_array function

前端 未结 11 610
故里飘歌
故里飘歌 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:34

    Say you want to use the in_array, here is how you can make the search case insensitive.

    Case insensitive in_array():

    foreach($searchKey as $key => $subkey) {
    
         if (in_array(strtolower($subkey), array_map("strtolower", $subarray)))
         {
            echo "found";
         }
    
    }
    

    Normal case sensitive:

    foreach($searchKey as $key => $subkey) {
    
    if (in_array("$subkey", $subarray))
    
         {
            echo "found";
         }
    
    }
    

提交回复
热议问题