sandy1028 Posted February 10, 2012 Share Posted February 10, 2012 Please tell me how to check the key based on any case. ow $sec will check for the exact case. How can I make it insensitive public static function getSMap($sec) { $sects = self::$Mappings; if(array_key_exists($sec, $Mappings)) { return $sects[$sec]; } static $Mappings = array( "BUINESS" => 'business', "TOP 100" => 'business'; "busine" => 'business'; } Quote Link to comment https://forums.phpfreaks.com/topic/256798-array_key_exists/ Share on other sites More sharing options...
kicken Posted February 10, 2012 Share Posted February 10, 2012 You can get a list of all the key names using array_keys, convert them all to lowercase using strtolower+array_map and then check if your value exists in that array. <?php function array_key_exists_case($arr, $keyName){ $allKeys=array_keys($arr); $allKeysLower=array_map('strtolower', $allKeys); $idx = array_search(strtolower($keyName), $allKeysLower); if ($idx===false){ return false; } else { return $allKeys[$idx]; } } That function will check if the key exists in a case insensitive manner. It will return false if it does not exist. If it does exist, it returns the key in the case it exists as so you can use that to access the value. Quote Link to comment https://forums.phpfreaks.com/topic/256798-array_key_exists/#findComment-1316477 Share on other sites More sharing options...
sandy1028 Posted February 10, 2012 Author Share Posted February 10, 2012 Thanks. Please tell me how to modify the above code without adding another function. Quote Link to comment https://forums.phpfreaks.com/topic/256798-array_key_exists/#findComment-1316480 Share on other sites More sharing options...
spiderwell Posted February 10, 2012 Share Posted February 10, 2012 just integrate the code give to you by kicken into your original function, but its a useful function, why not keep it seperate? if you tie it into another function it will be less resuseable Quote Link to comment https://forums.phpfreaks.com/topic/256798-array_key_exists/#findComment-1316487 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.