Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2008
    Posts
    61
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How do I get only a few results using foreach

    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)
    Last edited by WebmasterLULZ; 12-09-2008 at 06:23 PM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You can always just use break; to break out of the foreach loop.

  • #3
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    If you want to limit your foreach to say just a few loops put:

    PHP Code:
    if($x 3){
    break;
    }
    $x++; 

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •