php: Array keys case *insensitive* lookup?

前端 未结 12 1931
予麋鹿
予麋鹿 2020-12-25 10:30
$myArray = array (\'SOmeKeyNAme\' => 7);  

I want $myArray[\'somekeyname\'] to return 7.
Is there a way to do this

12条回答
  •  攒了一身酷
    2020-12-25 11:08

    I know this is old, but in case anyone else needs a quick easy way to do this without actually changing the original array:

    function array_key_i($needle, $haystack){
      $key=array_search(strtolower($search), array_combine(array_keys($array),array_map('strtolower', array_keys($array))));
      return ($key!==false);
    }
    
    $array=array('TeSt1'=>'maybe');
    $search='test1';
    
    array_key_i($search, $array); // returns true
    

提交回复
热议问题