![]() |
[solved] array modication during a foreach loop - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: [solved] array modication during a foreach loop (/showthread.php?tid=6504) |
[solved] array modication during a foreach loop - El Forum - 03-01-2008 [eluser]kevinprince[/eluser] I have been trying this afternoon to add a value to an array during a foreach loop, however it just doesnt seem to work. I investigated on php.net and foreach does not touch the original array just loads a copy into memory. There is then the usual mess of php.net explanations which I cant get my head around on how to solve it. My code is below, so if you have any ideas on how to solve it let me know its mighty annoying. Code: ///Get List of Events [solved] array modication during a foreach loop - El Forum - 03-01-2008 [eluser]xwero[/eluser] try this Code: ///Get List of Events [solved] array modication during a foreach loop - El Forum - 03-01-2008 [eluser]kevinprince[/eluser] Thanking you! Why cant people just explain these things simply, all the php.net examples were done using while and list. Marked as solved. [solved] array modication during a foreach loop - El Forum - 03-01-2008 [eluser]mironcho[/eluser] As of php5 you can use foreach value as reference: Code: foreach($eventlist as &$item): Better way to do this is by using index as xwero has shown or by using foreach key functionality: Code: foreach($eventlist as $key => $item): |