Re: each() bug? Can you try this to verify?

From: Date: Sat, 26 Aug 2000 01:08:16 +0000
Subject: Re: each() bug? Can you try this to verify?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-13767@lists.php.net to get a copy of this message
> <?php > > file://$array = array("hans"=>"brent"); > $array = array("hans"=>"brent","annie"=>"anne"); > > print sizeof($array) . "\n"; > > // loop, output data > > while(list($k,$v) = each($array)) { > print "k=v: $k=$v\n"; > } > > print sizeof($array) . "\n"; Add: reset($array); > > // loop again, should output the same data, but doesn't on my machine > > while(list($k,$v) = each($array)) { > print "k=v: $k=$v\n"; > } > > print sizeof($array) . "\n"; > > print "Where are the values of array on the second loop? GONE! Why?\n"; Incidentally, a wonderful feature of PHP 4 allows you to do the following to iterate through arrays: For associative arrays: foreach($array as $key => $value) { echo "<p>{$key}={$value}"; } For 0-based arrays: foreach($array as $element) { echo "<p>$element"; } But if you need to keep track of the index, the traditional C-method (for($i = 0; $i < sizeof($array); $i++) {...}) should be used. Dean.

« previous php.general (#13767) next »