First, the problem is related to all classes which implement ArrayAccess
it is not a special problem of SplFixedArray
only.
When you accessing elements from SplFixedArray
using the []
operator it behaves not exactly like an array. Internally it's offsetGet()
method is called, and will return in your case an array - but not a reference to that array. This means all modifications you make on $a[0]
will get lost unless you save it back:
Workaround:
$a = new SplFixedArray(5);
$a[0] = array(1, 2, 3);
// get element
$element = $a[0];
// modify it
$element[0] = 12345;
// store the element again
$a[0] = $element;
var_dump($a);
Here is an example using a scalar which fails too - just to show you that it is not related to array elements only.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…