Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-02-2010, 11:27 PM   PM User | #1
ole90
Regular Coder

 
Join Date: Jan 2007
Posts: 217
Thanks: 9
Thanked 0 Times in 0 Posts
ole90 is an unknown quantity at this point
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!
ole90 is offline   Reply With Quote
Old 03-02-2010, 11:59 PM   PM User | #2
bacterozoid
Regular Coder

 
bacterozoid's Avatar
 
Join Date: Jun 2002
Location: USA
Posts: 486
Thanks: 23
Thanked 35 Times in 35 Posts
bacterozoid is an unknown quantity at this point
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..
bacterozoid is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:18 AM.


Advertisement
Log in to turn off these ads.