|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-03-22 19:52 UTC] php dot net at alienbill dot com
nested while(list=each) produces weird results.
this code
<?php
$outsidearray = array("key1","key2");
$insidearray = array("0","1");
while(list(,$outerval) = each($outsidearray)){
//$placeholder = $insidearray;
while(list(,$innerval) = each($insidearray)){
print "inloop $innerval for $outerval<br>";
}
}
?>
only gets to key1 of the outer loop.
But if you uncomment the $placeholder line,
it works ok.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 07 15:00:01 2025 UTC |
Thanks for reporting. I verified with 4.2.0-dev & 4.0.6. This is _VERY_ bad bug... [root@dev etc]# php <?php $outsidearray = array("key1","key2"); $insidearray = array("0","1"); while(list(,$outerval) = each($outsidearray)){ //$placeholder = $insidearray; while(list(,$innerval) = each($insidearray)){ print "inloop $innerval for $outerval<br>"; } } ?> inloop 0 for key1<br> inloop 1 for key1<br> [root@dev etc]# php <?php $outsidearray = array("key1","key2"); $insidearray = array("0","1"); while(list(,$outerval) = each($outsidearray)){ $placeholder = $insidearray; while(list(,$innerval) = each($insidearray)){ print "inloop $innerval for $outerval<br>"; } } ?> inloop 0 for key1<br> inloop 1 for key1<br> inloop 0 for key2<br> inloop 1 for key2<br> [root@dev etc]# [root@dev etc]# php -v 4.2.0-devI forgot about reset() since behavior is inconsistent :( Anyway, this is still not right. Zend engine is allocating new zval for $insidearray while it should allocate $placeholder. --- $placeholder = $insidearray; while(list(,$innerval) = each($insidearray)){ print "inloop $innerval for $outerval<br>"; --- This bug causes nasty bug in script. If it is not easy to fix, may be we should suspend this bug?Both each() and foreach() uses internal hash position variable. Difference is foreach() reset internal hash position to the first element while each() does not. A problematic behavior is an assignment resets internal hash position. There are other cases programmer has to be careful about internal hash position and reference counting feature. In addition to that, it is _not_ intuitive that assignment as follows $arr = array('a', 'b', 'c'); does not reset hash posiiton, while following assignment does reset hash position. $arr2 = $arr1; Anyway, some internal hash posision behaviors are inconsistent and _not_ intuitive at all. This would be really hard to find if a user experience the problem. Therefore, it should be documented :)Oops, $arr = array('a', 'b', 'c'); does reset position :)