Jump to content

array_key_exists


sandy1028

Recommended Posts

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';
}

Link to comment
https://forums.phpfreaks.com/topic/256798-array_key_exists/
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/256798-array_key_exists/#findComment-1316477
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.