string) */ private $lookup = array(); /** * Holds the normal associative array between keys in correct case and values. * * Format: array(mixedCaseKey, value) * * @var array(string=>string) */ private $map = array(); /** * Constructs a new case insensitive associtive array formed around the array * $map with mixed case keys. */ public function __construct( array $map = array() ) { $this->map = $map; foreach ( $map as $key => $value ) { $this->lookup[strtolower( $key )] = $key; } } /** * Returns true if the $key exists in the array. * * @param string $key * @return bool */ public function offsetExists( $key ) { return array_key_exists( strtolower( $key ), $this->lookup ); } /** * Returns the value recognized with $key. * * @param string $key * @return mixed */ public function offsetGet( $key ) { $key = strtolower( $key ); if ( !array_key_exists( $key, $this->lookup ) ) { return null; } return $this->map[$this->lookup[$key]]; } /** * Sets the offset $key to the value $value. * * If it is a new entry the case in $key will be stored. If the $key exists already * using a case insensitive lookup the new spelling will be discarded. * * @param string $key * @param mixed $value * @return void */ public function offsetSet( $key, $value ) { $lowerKey = strtolower( $key ); if ( !array_key_exists( $lowerKey, $this->lookup ) ) { $this->map[$key] = $value; $this->lookup[$lowerKey] = $key; } else // use old case { $this->map[$this->lookup[$lowerKey]] = $value; } } /** * Unsets the key $key. * * @param string $key * @return void */ public function offsetUnset( $key ) { $key = strtolower( $key ); if ( array_key_exists( $key, $this->lookup ) ) { unset( $this->map[$this->lookup[$key]] ); unset( $this->lookup[$key] ); } } /** * Returns a copy of the associative array with the case of the keys preserved. * * @return array */ public function getCaseSensitiveArray() { return $this->map; } } ?>