Re: Putting a stop in a foreach

From: Date: Sun, 09 May 2004 15:34:35 +0000
Subject: Re: Putting a stop in a foreach
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-185744@lists.php.net to get a copy of this message
Simple! $i = 0; foreach ($foos as $foo) { // do stuff $i++; if ($i > 5) break; } Cheers "Verdon Vaillancourt" <vern@verdon.ca> wrote in message news:BCC3BBBF.DF40%vern@verdon.ca... > Hi :) > > I'm trying to put a stop in a foreach statement following the user > suggestion here, php.net/manual/en/control-structures.foreach.php > > Not really knowing what I am doing, I am running into some synatx problems. > I'm sure I'm doing something really stupid, can anybody point it out? > > This is the original statement that works... > > foreach ($this->_content as $item) { > if ($item['type'] == 'item'){ > $elements['ITEM_LINK'] = $item['link']; > $elements['ITEM_TITLE'] = $item['title']; > $elements["TARGET"] = $this->_target; > $items .= PHPWS_Template::processTemplate($elements, > "phpwsrssfeeds", "block_item.tpl"); > } > } > > > 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'){ > $elements['ITEM_LINK'] = $this->_content[$n] => > $item['link']; > $elements['ITEM_TITLE'] = $this->_content[$n] => $item['title']; > $elements["TARGET"] = $this->_target; > $items .= PHPWS_Template::processTemplate($elements, > "phpwsrssfeeds", "block_item.tpl"); > } > } > } > > Php doesn't like the syntax on any of the, > $this->_content[$n] => $item['type'] > , etc lines > > > TIA, > Verdon

« previous php.general (#185744) next »