|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2000-08-12 02:20 UTC] ronen at greyzone dot com
When I use a for construct to step through an associative array, next() will stop when it reaches a key equivalent to "0". "0" is treated like the end of the array, even if it is not.
SAMPLE CODE:
<?
$test[123]="one hundred twenty three";
$test[13]="thirteen";
$test[0]="zero";
$test[3]="three";
$test[11]="eleven";
// try the for construct:
for(reset($test);$key=key($test);next($test)) {
echo "TEST[$key]=".$test[$key]."<br>";
}
echo "************************************<br>";
// try the foreach construct
foreach($test as $key => $value) {
echo "TEST[$key]=$value<br>";
}
/***** THE OUTPUT IS:
TEST[123]=one hundred twenty three
TEST[13]=thirteen
************************************
TEST[123]=one hundred twenty three
TEST[13]=thirteen
TEST[0]=zero
TEST[3]=three
TEST[11]=eleven
****/
?>
Why does for not work, when foreach does?
Ronen.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Dec 13 19:00:01 2025 UTC |
It is not a bug, but a feature. When $key=key($test) matches false ("0" is considered false), the loop ends. That's normal behaviour.