View Full Version : How do I get only a few results using foreach

12-09-2008, 07:13 PM
Currently I am doing

foreach ($f as $val)
with $f having several arrays (which ranges from time to time due to another function i have setup).

How do i get it in forreach to only get a few $val (s)

12-09-2008, 07:28 PM
You can always just use break; to break out of the foreach loop.

12-10-2008, 01:03 PM
If you want to limit your foreach to say just a few loops put:

if($x > 3){

12-11-2008, 12:03 AM
One thing everyone is assuming is that the desired results are in sequential order from the start of the array. If its not, you use conditions with continues within you're foreach. From the sounds of it, I'd say the break is what you're looking at (as in its in the sequential order you're requiring).

Why not make use of a for loop instead? As soon as you need to describe the situation with the phrases 'limit' or 'range', a for loop is a better solution than a foreach loop. The purpose of the foreach is just as the name implies - for each.