note 52860 added to control-structures.foreach

From: Date: Sun, 15 May 2005 17:22:04 +0000
Subject: note 52860 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-89468@lists.php.net to get a copy of this message
another WARNING about report #26396 having status "wont fix": Beware of using foreach in recursive functions like... function sort_folders(&$items, $parent,$level) { foreach($items as $item) { if($item->parent == $parent) { print $item; // call recursively... sort_folders(&$items, $item, $level+1); } } } I struggled a few hours with this code, because I thought the passing the array by reference would be the problem. Infact you can only have ONE foreach for an array at any given time. A foreach inside another foreach does not work. The manual should definately give some hints about this behaviour. pixtur ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52860 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52860&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52860&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52860&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52860&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52860&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52860&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#89468) next »