Click to See Complete Forum and Search --> : stupid question (for every 8 results do x)


knowj
11-27-2007, 09:00 AM
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:

$z=7;
for ($i=0; $i<=count($array); $i++) {
if($i == $z) {
echo $array[$i];
$z=$z+8;
}
if ($i != $z) {
echo $array[$i];
}
}

jasonahoule
11-27-2007, 10:00 AM
I think if($i = $z) should read if($i == $z) otherwise it would always be true. Nevermind, you changed it.

werred
11-27-2007, 10:01 AM
Yeah, I've noticed the typo just after I've posted the message, but you were first to reply :)

TJ111
11-27-2007, 10:15 AM
If you need 0 as well, and the pattern isn't multiples of 8 (if it is, just adjust accordingly), then something like this would work as well:

$array = array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33);
$z=0;
for ($i=0; $i<=count($array); $i++) {

if($i == $z) {

echo "<b>{$array[$i]}</b>&nbsp;&nbsp;<i>$i</i><br>";
$z = ($z == 0) ? 7 : ($z*2) +1 ;
}
if ($i != $z) {
echo $array[$i] . "<br>";
}
}

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:

for($ix = 0; array_key_exists($ix, $array); $ix += 8)
{
echo "Result $ix = {$array[$ix]}<br>\n";
}

If instead you want to do 0, 7, 15, 23,..., then:

echo "Result 0 = {$array[0]}<br>\n";
for($ix = 7; array_key_exists($ix, $array); $ix += 8)
{
echo "Result $ix = {$array[$ix]}<br>\n";
}

bokeh
11-28-2007, 09:39 AM
array_key_exists($ix, $array);I've never really understood why that function was invented. Why not just use isset($array[$ix]) ?