Say I have the following code:
<?php
class test implements ArrayAccess {
var $var;
function __construct()
{
$this->var = array(
'a' => array('b' => 'c'),
'd' => array('e' => 'f'),
'g' => array('h' => 'i')
);
}
function offsetExists($offset)
{
return isset($this->var);
}
function offsetGet($offset)
{
return isset($this->var[$offset]) ? $this->var[$offset] : null;
}
function offsetSet($offset, $value)
{
if (is_null($offset)) {
$this->var[] = $value;
} else {
$this->var[$offset] = $value;
}
}
function offsetUnset($offset)
{
unset($this->var[$offset]);
}
}
$test = new test();
$test['a']['b'] = 'zzz';
print_r($test->var);
What I'd want that to do is to display something like this:
Array
(
[a] => Array
(
[b] => zzz
)
[d] => Array
(
[e] => f
)
[g] => Array
(
[h] => i
)
)
What it actually displays is more like this:
Array
(
[a] => Array
(
[b] => c
)
[d] => Array
(
[e] => f
)
[g] => Array
(
[h] => i
)
)
ie. $test['a']['b'] is unchanged.
Any idea how I can make it changeable using that syntax? I could assign $test['a'] to a variable and then do $temp['b'] = 'zzz'; and then do $test['a'] = $temp; but idk - that seems excessive.