You can implement your own class that "implements ArrayAccess"
For such a class, you can manually handle this behavior
UPD : implemented just for fun
class MyArray implements ArrayAccess
{
private $data;
private $keys;
public function __construct(array $data)
{
$this->data = $data;
$this->keys = array_keys($data);
}
public function offsetGet($key)
{
if (is_int($key))
{
return $this->data[$this->keys[$key]];
}
return $this->data[$key];
}
public function offsetSet($key, $value)
{
throw new Exception('Not implemented');
}
public function offsetExists($key)
{
throw new Exception('Not implemented');
}
public function offsetUnset($key)
{
throw new Exception('Not implemented');
}
}
$array = new MyArray(array(
'test' => 'thing',
'blah' => 'things'
));
var_dump($array[0]);
var_dump($array[1]);
var_dump($array['test']);
var_dump($array['blah']);
source
share