-3

I have an array and I would like to take action on every iteration

for($i = 0; $i < count($array); $i++) {
      0,1,3...10 
     // Execute
     $create->save(); 
      11, 12, 13... 20
     // Execute
     $create->save(); 
}
Foued MOUSSI
  • 4,643
  • 3
  • 19
  • 39
Rafael Moura
  • 1,247
  • 2
  • 14
  • 26
  • 2
    Does this answer your question? [php modulus in a loop](https://stackoverflow.com/questions/8135404/php-modulus-in-a-loop) – noetix Feb 23 '20 at 23:03

2 Answers2

1

You can use the modulo of 10.

for($i = 0; $i < count($array); $i++) {
    if($i%10 == 0{
        $create->save();
    }
}
Andreas
  • 23,610
  • 6
  • 30
  • 62
  • What this says is: if the iteration value is divisible by x (10) with no remainder (so 12 is not divisible by 10 *cleanly* as it leaves a remainder of 2). Then perform action y :) – Mr_DW_Brighton Feb 25 '20 at 11:35
0

Something like this?

$n = 10;
for($i = 0; $i < count($array); $i++) {
    if($n == $i) {
        $create->save();
        $n = $n + 10;
    }
}

But here in your loops 10 != 10, because is 11 loop. If you need to execute at every 10 loop, then $n = 9 or $i = 1;

Salines
  • 5,674
  • 3
  • 25
  • 50