Re: endless while loop

From: Date: Thu, 06 Jan 2005 16:13:52 +0000
Subject: Re: endless while loop
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-205736@lists.php.net to get a copy of this message
"Jason Wong" <php-general@gremlins.biz> wrote in message news:200501061706.53390.php-general@gremlins.biz... > On Thursday 06 January 2005 05:17, Gunter Sammet wrote: > >> I am having some trouble getting the following code to work: >> >> if (is_array($bundle_attributes)) { >> >> reset($bundle_attributes); >> >> while (list($option, $value) = each($bundle_attributes)) { >> >> reset($value); >> >> while (list($option2, $value2) = each($value)) { >> >> $this->contents[$products_id]['attributes'][$option] = $value; >> >> if (tep_session_is_registered('customer_id')) tep_db_query("insert into >> " >> . >> TABLE_CUSTOMERS_BASKET_BUNDLE_ATTRIBUTES . " (customers_id, products_id, >> sub_product_id, products_options_id, products_options_value_id) values >> ('" >> . (int)$customer_id . "', '" . tep_db_input($products_id) . >> "', '" . >> $option . "', '" . (int)$option2 . "', '" . >> (int)$value2 . "')"); >> >> } >> >> } >> >> } >> >> } >> >> It gives me an endless loop in the second while loop. It seems like it >> doesn't move the pointer to the next value in the $option array. > > But isn't the "next value" of $option controlled by the outer while-loop? > >> When I do >> a print in that loop, I am getting the key value pair for the first >> element >> in the $option array. > > So inside the inner while-loop, $option should never change. > >> Changing it to a for loop works. Any ideas why this >> happens? > > No. But I can tell you that: > > 1) the reset() aren't necessary > 2) even better, use a foreach-loop instead of while-loop > > -- > Jason Wong -> Gremlins Associates -> www.gremlins.biz > Open Source Software Systems Integrators > * Web Design & Hosting * Internet & Intranet Applications Development * > ------------------------------------------ > Search the list archives before you post > http://marc.theaimsgroup.com/?l=php-general > ------------------------------------------ > New Year Resolution: Ignore top posted posts Hi Jason: Thanks for the reply. I added some of the reset in just to make sure the pointer isn't somewhere else through earlier iterations/to make sure it doesn't make a difference. Will play around with the foreach as soon as I have some time. Reading through php.net it seems like there are a few pitfalls (as there can be with while) to watch out for. Found out what the problem was after I posted. Reading through the function manual for each I found the following: <snip> Caution Because assigning an array to another variable resets the original arrays pointer, our example above would cause an endless loop had we assigned $fruit to another variable inside the loop. </snip> That's what I did. Didn't had to but it has been a copy and paste from some other code I haven't cleaned up. Good thing on it is, that I now know that I can't do this. Thanks again! Gunter

« previous php.general (#205736) next »