basically I need to loop through an array and on 0, 7, 15 etc... echo part of the array.
for ($i=0; $i<=count($array); $i++)
{
if (condition)
{
do this
}
do this
}
werred
11-27-2007, 09:57 AM
Which part of array do you need to print?
If it's the value assigned to that index (7,15) you'd like the following way:
Edit: I'm an idiot, didn't even read the whole title :rolleyes: . Make it this:
$z += ($z == 0) ? 7 : 8 ;
MrCoder
11-27-2007, 10:36 AM
Use MOD... %
knowj
11-27-2007, 11:36 AM
thanks for the help. I would use the Modulus but as its starting from 0 it would cause issues. or im just being thick in the implementation of it.
MQuimby
11-27-2007, 11:58 AM
thanks for the help. I would use the Modulus but as its starting from 0 it would cause issues. or im just being thick in the implementation of it.
Add 1 to z then check the Modulus
felgall
11-27-2007, 01:00 PM
No need to add anything before checking the modulus, just check it against whatever value is the appropriate one
$z % 8 == 0
to start from zero
$z % 8 == 1
to start from one
MQuimby
11-27-2007, 01:07 PM
I think for readability down the road sake, adding 1 might be easier, but not necessary.
I believe what the author wanted was action at 0,7, 15, etc.
Modulus 8 of 0 is 0, Mod 8 of 7 is 7, Mod 8 of 15 is 7
Mod 8 of (0+1) is 1, Mod 8 of (7+1) is 0, Mod 8 of (15+1) is 0
Actually, in either event, you'd need an exception for the first case...
NogDog
11-27-2007, 02:03 PM
Problem number one is to clarify the requirement. The thread title says "every 8 results do x", but then the example says on 0, 7, 15,... which is actually every 7 results and then every 8 results. Every 8 results would be 0, 8, 16, etc.; or it could be 7, 15, 23, etc.; but 0, 7, 15, [and presumably 23 next] is kind of a combination: processing result 0, then starting with result 7 process every 8th result.
Anyway, if you actually want every 8th result starting with a particular index (let's use 0 for this example), you can do it all within the for loop definition: