Referential Foreach?
Posted: Wed May 31, 2006 11:52 am
Not sure if this is PHP Theory or not, but is there such a thing as a referential foreach loop? What I mean is, when I do a foreach loop, currently (at least for PHP4) it accesses the 'each' item as a copy, and not a reference. I know PHP5 you can do something like:
You can then assign values to anything that might potentially be in item, and it will update the parent array, whereas in PHP4, you can't even do the above (take out the ampersand). But with this, you can't do something like:
It will "work", as $item['total'] can be used within that loop to access that calculated value, but I want to be able to access a calculated value later on. Is there an easy workaround to this, or should I just stick to my for($i=0) loops?
Thanks
- Moose
Code: Select all
foreach($array as &$item) {
// whatever
}Code: Select all
foreach($array as $item) {
$item['total'] = $item['count'] * $item['price'];
}Thanks
- Moose