dongxin0031 2019-02-04 14:30
浏览 274
已采纳

如何在foreach循环运行的每50次增加一个值1

How can I increment a value in a foreach loop for every 50 times the loop runs.

<?php

$counter = 1; 
foreach ($numbers as $num) {
      //For the first 50 times the loop runs, $counter = 1. For every 50 runs, increment by 1
$counter = 1;

//if loop has run more than 50 times, increment $counter to 2 


}
?>
  • 写回答

4条回答 默认 最新

  • dongzha5934 2019-02-04 14:40
    关注
            <?php 
        $counter = 0;
        $value = 50; // Intial position
        $numbers = 230 // Lets say you have total 230 iterations.
        for ($i = 0 ; $i <= $numbers ; i++)
        {
         if($i == $value) // if 50 counter is increased and we are setting the value to 100
    {
    $counter += 1;
    $value = $value * 2;
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?