It seems like I can\'t iterate by reference over values in an SplFixedArray:
$spl = new SplFixedArray(10);
foreach ($spl as &$value)
{
$value = \"string\
Any workaround?
Short answer: don't iterate-by-reference. This is an exception thrown by almost all of PHP's iterators (there are very few exceptions to this exception); it isn't anything special for SplFixedArray
.
If you wish to re-assign values in a foreach
loop, you can use the key just like with a normal array. I wouldn't call it a workaround though, as it is the proper and expected method.
$spl = new SplFixedArray(10);
foreach ($spl as &$value)
{
$value = "string";
}
var_dump($spl);
$spl = new SplFixedArray(10);
foreach ($spl as $key => $value)
{
$spl[$key] = "string";
}
var_dump($spl);