I'm doing a lot of iterating arrays by reference - typically arrays of objects (Using PHP 4.3.8, but will consider PHP5 ultimately).
Now I discovered that foreach() does not iterate by reference, hence
foreach ($objectlist as $obj) {
$obj->MutatingMethod();
}
Has no effect on the contents of $objectlist.
I've come up with the following two solutions, neither of which I like very much:
1.
foreach ($objectlist as $key => $junk) {
$obj = & $objectlist[$key];
$obj->MutatingMethod();
}
2.
foreach (array_keys($objectlist) as $key) {
$obj = & $objectlist[$key];
$obj->MutatingMethod();
}
I don't like either of them, they look messy to me.
Any better ideas?
Mark