note 50789 deleted from control-structures.foreach by aidan
| From: | aidan@php.net | Date: | Sat, 12 Mar 2005 00:37:51 +0000 |
| Subject: | note 50789 deleted from control-structures.foreach by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-86313@lists.php.net to get a copy of this message | ||
Note Submitter: gilthansNOSPAAM at gmailSPAAMBLOCK dot com
----
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.
?>