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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Skipping array values

    Okay, I can't seem to get my head around this method.

    I have an array:

    PHP Code:
    $array = array(1,2,3,4,5,6,7,8,9,10); 
    and in a for loop, I want to skip the array every 2 digits so the array would output.

    1 , 2 , 5, 6, 9, 10

    PHP Code:
    for($i=0;$i<$arraylength;$i++) {

    How would I do this within a for loop?

    Thanks in advance!

  • #2
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts

    Macintosh

    This might work...though there could be easier ways. You're always skipping multiples of 4 and numbers 1 less than multiples of 4.

    Do you know how modulus (%) works?

    PHP Code:
    for($i=0;$i<$arraylength;$i++) {
        if(
    $i === || ($i+1) % === 0) {
            continue;
        }

    Last edited by bacterozoid; 03-03-2010 at 12:01 AM.


  •  

    Posting Permissions

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