Re: [PHP] Putting a stop in a foreach
* Thus wrote Verdon Vaillancourt ([EMAIL PROTECTED]): > 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." -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Putting a stop in a foreach
Hmm, yes I see. Thank you. I guess what I should look at then is getting the key value (internal counter/pointer) in the array going to the foreach. Like this example... $a = array( 1,2,3,17 ); $i =0;/* for illustrative purposes only */ foreach ( $a as $v ) { echo "\$ a[$i ]=> $v. \n "; $i ++; } Once I have that key value (internal counter) I should be able to do something with it? What I really want to do (since I can't easily get at the query building the array being passed to the foreach) is get the foreach to stop outputting results at a certain limit. Thanks again, Verdon On 5/9/04 11:34 AM, "Matt Schroebel" <[EMAIL PROTECTED]> wrote: > It's good to understand what foreach is doing: > $car['GM'] = 'Impala'; > $car['TOYOTA'] = 'Corolla'; > $car['HONDA'] = 'Civic'; > > foreach ($car as $manufacturer => $ model) { > echo "$manufacturer - $model \n"; > } > ?> > > Results: > GM - Impala > TOYOTA - Corolla > HONDA - Civic > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Putting a stop in a foreach
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php