PHP access value using case insensitive index

橙三吉。 提交于 2019-12-23 02:52:45

问题


how to access a value in php array using case insensitive index. like

$x = array('a'=>'ddd');

any function to access it using $x['A'];


回答1:


That is just not the way strings / array work, in PHP.

In PHP, "a" and "A" are two different strings.
Array keys are either integers or strings.

So, $a["a"] and $a["A"] point to two distinct entries in the array.


You have two possible solutions :

  • Either always using lower-case (or upper-case) keys -- which is probably the best solution.
  • Or search through all the array for a possible matching key, each time you want to access an entry -- which is a bad solution, as you'll have to loop over (in average) half the array, instead of doing a fast access by key.


In the first case, you'll have to use strtolower() each time you want to access an array-item :

$array[strtolower('KEY')] = 153;
echo $array[strtolower('KEY')];

In the second case, mabe something like this might work :
(Well, this is a not-tested idea ; but it might serve you as a basis)

if (isset($array['key'])) {
    // use the value -- found by key-access (fast)
}
else {
    // search for the key -- looping over the array (slow)
    foreach ($array as $upperKey => $value) {
        if (strtolower($upperKey) == 'key') {
            // You've found the correct key
            // => use the value
        }
    }
}

But, again it is a bad solution !




回答2:


I would use OOP here, either extend ArrayObject or implement ArrayAcess and then in the offset methods use relevant string functions to match either case.

This is AFAIK the only way to be able to access it using $x['A']; syntax.

Of course I show no interest here in optimizing for speed. You should have to decide if speed or readable code is your primary goal.

Example:

class MyArray extends ArrayObject {
    public function offsetGet($index) {
        if ($this->offsetExists($index)) {
            return parent::offsetGet($index);
        }
        if ($this->offsetExists(strtolower($index))) {
            return parent::offsetGet(strtolower($index));
        }
        if ($this->offsetExists(strtoupper($index))) {
            return parent::offsetGet(strtoupper($index));
        }
        return null;
    }

}

$x = array('a'=>'ddd');
$x = new MyArray($x);
echo $x['A'];

Output:

ddd



回答3:


function array_keys_to_lower($array)
{
  $lc_array = array();

  foreach ($array as $key => $value)
  {
    $lc_array[strtolower($key)] = $value;
  }

  return $lc_array;
}

function get_value($array, $index)
{
  $lc_array = array_keys_to_lower($array);

  return $lc_array[strtolower($index)];
}



回答4:


Easy.
Don't do it.

Obfuscating your own code is not a brilliant idea.
Just follow syntax rules and you'll be okay.




回答5:


You have to write your own function for that, pseudocode:

function getElem(index, array){
  return array[toLower(index)];
}



回答6:


$index = "a";

$index = strtoupper($index);

$ret = $a[$index];



回答7:


I guess without changing your current array usage would be something like this:

$x = array('a'=>'ddd');

echo isset($x['a']) ? $x['a'] : $x['A'];

You could have this a function like following:

$x = array('a'=>'ddd');

echo getIndex($x, 'a');

// return either the value of $x['a'], $['A'] or null
function getIndex($array, $index) {
    return isset($x[strtolower($index)]) ? $x[strtolower($index)] : (isset($x[strtoupper($index)]) ? $x[strtoupper($index)] : null);
}



回答8:


This might be a little late, but I think what you are trying to accomplish can be done easily:

$key = 'Key';
$array[$key] = 153;

$array = array_change_key_case($array)
echo $array[strtolower($key)];



回答9:


You can use array_change_key_case to change the key case of your array, convert all keys to lower case or upper case and have your peace of mind.

example:

$headers = getallheaders();
$headers = array_change_key_case($headers, CASE_UPPER);
    if (isset($headers['AUTHENTICATION'])) {
         // do your thing
    }

http://php.net/manual/en/function.array-change-key-case.php



来源:https://stackoverflow.com/questions/5404742/php-access-value-using-case-insensitive-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!