...

View Full Version : Skipping array values



ole90
03-02-2010, 11:27 PM
Okay, I can't seem to get my head around this method.

I have an array:


$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


for($i=0;$i<$arraylength;$i++) {
}

How would I do this within a for loop?

Thanks in advance!

bacterozoid
03-02-2010, 11:59 PM
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?


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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum