Option 1 - change the way you create the array
You cannot do this without linearly searching or modifying the original array. The most efficient approach would be to use strtolower on the keys when you insert AND when searching for values.
$myArray[strtolower('SOmeKeyNAme')]=7; if (isset($myArray[strtolower('SomekeyName')])) { }
If it is important for you to keep the original case of the key, you can save it as an additional value for this key, for example.
$myArray[strtolower('SOmeKeyNAme')]=array('SOmeKeyNAme', 7);
Option 2 - create a secondary display
How did you update the question to suggest that it would be impossible for you, how about creating an array that provides comparisons between lowercase and lowercase versions?
$keys=array_keys($myArray); $map=array(); foreach($keys as $key) { $map[strtolower($key)]=$key; }
Now you can use this to get the case sensitive key from the bottom
$test='somekeyname'; if (isset($map[$test])) { $value=$myArray[$map[$test]]; }
This avoids the need to create a full copy of the array with a key with a lower value, which is actually the only way to do this.
Option 3 - Create a copy of the array
If a full copy of the array is not a concern, you can use array_change_key_case to create a copy with the lower case keys.
$myCopy=array_change_key_case($myArray, CASE_LOWER);
Paul Dixon Nov 21 '10 at 19:59 2010-11-21 19:59
source share