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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.