Ben Liu wrote:
The second thing to do is to make the foreach work
on references instead of copies. I actually think that since what we
have in our function is now a reference, the foreach WILL NOT copy it.
Anyone have any ideas on this? Is the foreach loop in fact copying
$workArr? And how could we tell (test for it) if it were?
I should note that the DEFAULT behaviour of foreach is to make a copy of
the array you pass it. What I'm saying is that, now that we're passing
it a reference to an array instead of an actual array, is that enough to
stop it from copying? The manual seems to indicate that, but is a bit vague.
But if it still does, there is always the fallback of doing a foreach on
array_keys($workArr). Then each $value would be the key, which you'd use
to reference $workArr.
I'm sorry, you lost me here a bit.
I mean, do this:
foreach ( array_keys($workArr) as $key ) {
echo $workArr[$key]['foo'];
}
instead of this:
foreach ( $workArr as $key => $value ) {
echo $value['foo'];
}
Both let you do the same thing, you just reference data differently, and
the first example involves working with the original array, so foreach
has no chance to copy it. HOWEVER, if my above supposition about foreach
not copying a reference is correct, you wouldn't need to do this. It's
just a backup plan.
Regards, Adam.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php