php: Array keys case *insensitive* lookup?

前端 未结 12 1840
予麋鹿
予麋鹿 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:02

    From the PHP site

    function array_ikey_exists($key, $haystack){
        return array_key_exists(strtolower($key), array_change_key_case($haystack));    
    }
    

    Referance: http://us1.php.net/manual/en/function.array-key-exists.php#108226

    0 讨论(0)
  • 2020-12-25 11:04

    I just had same problem and I could not change original array. I use few array functions for it.

    Parameters

    $search = "AbCd";
    $array = array("AbcD"=>"11","Bb"=>"22");
    

    Solution

    $lower_search = strtolower($search);
    $array_of_keys = array_map("strtolower",array_keys($array));
    $idx = array_search($lower_search,$array_of_keys);
    if($idx !== FALSE)
        echo array_values($array)[$idx];
    

    Make it shorter

    if(($idx=array_search(strtolower($search),array_map("strtolower",array_keys($array))))!==FALSE)
        echo array_values($array)[$idx];
    
    0 讨论(0)
  • 2020-12-25 11:06

    In my case I wanted an efficient workaround where my program was already creating the array using a foreach loop from customer data having unknown case, and I wanted to preserve the customer's case for later display in the program.

    My solution was to create a separate array $CaseMap to map a given lowercase key to the mixedcase key used in the array (irrelevant code is omitted here):

    $CaseMap=[];
    foreach ($UserArray as $Key=>$Value)
        $CaseMap[strtolower($Key)]=$Key;
    

    Then lookup is like this:

    $Value=$UserArray[$CaseMap("key")];
    

    and the memory overhead is just the $CaseMap array, which maps presumably short keys to short keys.

    I'm not sure if PHP has a more efficient way to generate $CaseMap in the case where I'n not already using foreach.

    0 讨论(0)
  • 2020-12-25 11:08

    I know this is an older question but the most elegant way to handle this problem is to use:

    array_change_key_case($myArray); //second parameter is CASE_LOWER by default
    

    In your example:

    $myArray = array ('SOmeKeyNAme' => 7);
    $myArray = array_change_key_case($myArray);
    

    Afterwards $myArray will contain all lowercase keys:

    echo $myArray['somekeyname'] will contain 7
    

    Alternatively you can use:

    array_change_key_case($myArray, CASE_UPPER);
    

    Documentation be seen here: http://us3.php.net/manual/en/function.array-change-key-case.php

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-25 11:09

    I also needed a way to return (the first) case-insensitive key match. Here's what I came up with:

    /**
     * Case-insensitive search for present array key
     * @param string $needle
     * @param array $haystack
     * @return string|bool The present key, or false
     */
    function get_array_ikey($needle, $haystack) {
        foreach ($haystack as $key => $meh) {
            if (strtolower($needle) == strtolower($key)) {
                return (string) $key;
            }
        }
        return false;
    }
    

    So, to answer the original question:

    $myArray = array('SOmeKeyNAme' => 7);
    $test = 'somekeyname';
    $key = get_array_ikey($test, $myArray);
    if ($key !== false) {
        echo $myArray[$key];
    }
    
    0 讨论(0)
提交回复
热议问题