$myArray = array (\'SOmeKeyNAme\' => 7);
I want $myArray[\'somekeyname\']
to return 7
.
Is there a way to do this
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
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];
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.
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
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
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];
}