note 54311 deleted from control-structures.foreach by danbrown
| From: | danbrown@php.net | Date: | Tue, 15 Mar 2011 13:45:31 +0000 |
| Subject: | note 54311 deleted from control-structures.foreach by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177892@lists.php.net to get a copy of this message | ||
Note Submitter: barnacle83-phpnotes at yahoo dot com
----
Here are various ways I've seen to iterate arrays by reference in PHP4.
Based on my tests, I have placed them in order of fastest to slowest.
Benchmarking tool I used:
http://phplens.com/phpeverywhere/phpe-2004.htm#a3297
http://phplens.com/lens/dl/JPBS.zip
<?php
// --------------------------------------------------------------------
foreach ( array_keys($myArray) as $key ) {
$element =& $myArray[$key];
...
}
// --------------------------------------------------------------------
foreach( $myArray as $key => $element ) {
$element =& $myArray[$key];
...
unset($element);
}
// --------------------------------------------------------------------
reset($myArray);
while ( list($key) = each($myArray) ) {
$element =& $myArray[$key];
...
unset($element);
}
?>
Andrew