Click to See Complete Forum and Search --> : PHP4: Iterate object by reference
nebulom
Jul 20th, 2007, 03:57 AM
I know I know, 4 loses support next year but I couldn't convince my co-developers to migrate from 4 to 5 so I'm stuck; we are stuck. Anyway, I need help with
foreach ($someArray as $someObject) {
// Do Changes to $someObject
}
I can't pass the changes back to the array... Help anyone? Thanks.
nebulom
Jul 20th, 2007, 04:00 AM
Solved it by for ($i = 0; $i < count($someArray); $i++) and $someObject = &$someArray[$i] but can't I do it with foreach? It's more neat that way.
penagate
Jul 20th, 2007, 09:16 AM
It's the same!
foreach ($c as &$v);
foreach ($c as $k => &$v);
However, PHP 5 passes objects by reference by default anyway, so this syntax is only necessary in PHP 4. The only use for it in PHP 5 is when dealing with collections of primitives, which aren't passed by reference.
nebulom
Jul 22nd, 2007, 07:35 PM
Thanks for the reply P. That gives Parse error: parse error, unexpected '&', expecting T_VARIABLE or '$'... on PHP4.4.1. Anyway I can still use the good for loop so it's semi-resolve now.
penagate
Jul 22nd, 2007, 08:37 PM
Oh, that was added in PHP 5. My mistake. Sorry.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.