note 50789 added to control-structures.foreach

From: Date: Thu, 10 Mar 2005 01:42:22 +0000
Subject: note 50789 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-86184@lists.php.net to get a copy of this message
Please note that while(list($key, $value) = each($array)) is NOT completely identical in functionality to foreach. Note the next code: <?php // The idea is that each $value will only run once. foreach($array as $value){ $array = array_diff($array, Array($value)) //... } //This does NOT work. foreach does not run through the //actual array, but creates a different one to run through, //so changes in the array inside the foreach do not affect //it. while(list(,$value) = each($array)){ $array = array_diff($array, Array($value)); //... } //This, however, will work as expected. Each $value will be //looped only once. In bigger arrays, where a value may be //repeated many times, this while() is faster than a foreach //that checks every loop weather that $value was already //used. ?> ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50789 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50789&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50789&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50789&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50789&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50789&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50789&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#86184) next »