1

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
  • $x = array(strtoupper('a')=>'ddd'); $y = $x[strtoupper('A')]; Commented Mar 23, 2011 at 11:51

9 Answers 9

6

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 !

Sign up to request clarification or add additional context in comments.

1 Comment

There is an extra "$" before the "as" in the loop. otherwise looks good!
4

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

2 Comments

I suppose someone didn't like my answer. I'd appreciate if someone could tell me what's wrong with it.
The idea being? My code, the reasoning behind it, or the original problem? If it is my code, what are the possible ways of improving it?
2
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)];
}

Comments

2

Easy.
Don't do it.

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

1 Comment

People in this thread should follow this advice. Calling array_change_key_case on anything causes long term problems on any code base as there's a need to add strtolower calls all over the place, and soon it starts creeping in unrelated parts of the codebase (which might not be case insensitive).
1

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

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

Comments

1
$index = "a";

$index = strtoupper($index);

$ret = $a[$index];

Comments

1

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

Comments

1

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)];

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.