Re: PHP 4.0 Bug #5270: Nested foreach() using a copy of the same array does not work properly.
| From: | Daniel Beckham | Date: | Wed, 28 Jun 2000 21:03:26 +0000 |
| Subject: | Re: PHP 4.0 Bug #5270: Nested foreach() using a copy of the same array does not work properly. | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-22672@lists.php.net to get a copy of this message | ||
Well, seeing as how php's array usage is a bit screwy (relative to my opinion of how it should work), that does make sense. The second foreach() statement is resetting the array pointer php uses for that array so that it can loop through each element. Now, why that happens on a copy is a mystery.
While I'm on that subject... Why is php a array pointer Nazi? It seems that a pointer is assigned to an array at birth. From then on.. any function that touches that array can possibly reset, move or otherwise wreak havoc on that pointer. Wouldn't it make more sense to temporarily assign a new pointer to the array in the context of the current loop it's in? That way, you could nest any number of loops without fear you will alter how the top loop works.
By making sense, I mean from the php programmer's standpoint, not the internals of how arrays work in the zend engine.
Daniel
At 10:37 PM 06/28/2000 +0200, you wrote:
I´d posted a similar bug (the same bug(?)) before, but I could not find that damn bug report again, you don´t need to copy $test, it works (bugs) too if both statements work on $test... summary: php seems to go wrong too if it´s the same variable danbeck@dealnews.com wrote:--From: danbeck@dealnews.comOperating system: Redhat 6.1PHP version: 4.0 Latest CVS (29/06/2000) PHP Bug Type: Scripting Engine problem Bug description: Nested foreach() using a copy of the same array does not work properly.The following code does not work properly. $test = $test2 = array (0,1,2,3,4,5,6,7,8,9); foreach($test as $a) {foreach($test2 as $b) { echo "$a : $b\n"; }} The first foreach() only happens once. I understand that foreach() will reset the array pointer but why does using the copy also seem to do the same thing to the original array? Shouldn't a copy be just that.. a copy? It seems that it's more of a reference than anything.o----------0-¬---------O-·---¬----o---®-----o o O ° . | http://www.kiffen.de | pRoteçt y0ur bRaín |0 O ° ¤ ° ·0°·³°²'²³-¹'³´³°^°³~³²³°'³²²¨³²^³¹³²°²³`³º³°Þ ° o © ° . ·| psychedelic experience | gott@kiffen.de | O ° o °o-¬--o--0-----©-·--O-----o-----0-¤----------o 0 ° · ° . ¤ · -- PHP Development Mailing List <http://www.php.net/> To unsubscribe, e-mail: php-dev-unsubscribe@lists.php.net For additional commands, e-mail: php-dev-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net