php: Array keys case *insensitive* lookup?

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

提交回复
热议问题