php: Array keys case *insensitive* lookup?

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

    You can lowercase your keys when assigning them to the array and also lowercase them when looking up the value.

    Without modifying the array, but the whole data structure:

    A really cumbersome way involves creating magic getter/setter methods, but would it really be worth the effort (note that the other methods have to be implemented too)?

    m_values = array();
      } 
    
      public function __get($key)
      { 
        return array_key_exists($key, $this->m_values) ? $this->m_values[$key] : null;
      } 
    
      public function __set($key, $value)
      { 
        $this->m_attributes[$key] = $value;
      } 
    } 
    

提交回复
热议问题