Re: Putting a stop in a foreach

From: Date: Sun, 09 May 2004 16:13:43 +0000
Subject: Re: Putting a stop in a foreach
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-185748@lists.php.net to get a copy of this message
* Thus wrote Verdon Vaillancourt (vern@verdon.ca): > Hi :) > > This is the original statement that works... > > foreach ($this->_content as $item) { > if ($item['type'] == 'item'){ > .. ok. good so far. > > > This is my attempt to count items and put a stop in the foreach so it only > returns 5 items. > > foreach ($this->_content as $n => $item) { > if ($n=="5") { > break; > } else { > if ($this->_content[$n] => $item['type'] == 'item'){ I'm not sure why you changed all this. All you have to do is a simple foreach loop with a counter, and break when the counter reaches your condition: $need = 5; /* how many we want */ foreach($this->_content as $index => $item) { if ($item['type'] == 'item') { //... /* only decrement if its an 'item' */ $need--; } if (! $need) break; /* we got everything we needed */ } Curt -- "I used to think I was indecisive, but now I'm not so sure."

« previous php.general (#185748) next »