简体   繁体   English

每3个迭代中断一次while循环

[英]Break while loop every 3 iterations

I am using this PHP to generate a list from 0-9. 我正在使用此PHP从0-9生成列表。

$counter = 0; 
WHILE ($counter < 10) 
{ 
  print "counter is now " . $counter . "<br>"; 
  $counter++; 
}

I'd like to change how this works. 我想更改其工作方式。 Every 3rd iteration , I'd like to wrap my printed text in a <div> if possible. 3次迭代 ,如果可能的话,我想将打印的文本包装在<div>

So eventually my outputted code would be: 所以最终我的输出代码将是:

<div>
counter is now 0
counter is now 1
counter is now 2
</div>
<div>
counter is now 3
counter is now 4
counter is now 5
</div>
<div>
counter is now 6
counter is now 7
counter is now 8
</div>
<div>
counter is now 9
</div>

With modulus operator you can split your output every 3 iterations but you still need to check for limit values that could generate empty div blocks 使用模数运算符,您可以每3次迭代拆分一次输出,但是您仍然需要检查可能生成空div块的limit

<?php

$counter = 0; 
$limit = 10;

print "<div>\n";

while ($counter < $limit) { 
  print "counter is now " . $counter . "<br>\n"; 

  if (++$counter % 3 === 0 && $counter < $limit) {
    print "</div>\n<div>\n";

  }
}
print "</div>\n";

Use a modulus to do it 使用模数来做

while($counter < 10) {
   if($counter % 3 == 0) {
       //Do something for the third row
   }
   $counter++;
}

Try with: 尝试:

$counter = 0;
$block_count = 0;
echo "<div>";
while ($counter < 10) {
if ($block_count === 3) {
    echo "</div><div>";
    $block_count = 0;
}
  echo "counter is now " . $counter . "<br>";
  $block_count++;
  $counter++; 
}
echo "</div>";

i don't know php but i think the logic is the same : 我不知道php,但我认为逻辑是相同的:

$counter2 =0; //count to 10
$stopIteration =10;

WHILE($counter2<$stopIteration){ 
    print "<div>";
    $counter1 =0; //count to 3
    WHILE($counter1<3){
        print "counter is now".$counter2."<br>";
        $counter1++;
        $counter2++;
    }
    print "</div>"; 
} 

I have found that best failsafe approach for this kind of task would be group all data (in this case all 10 your printed strings) in array, than split this array in chunks http://php.net/manual/en/function.array-chunk.php , and then you can work with this new array, because it is divided in chunks, where each chunk is not greater than for example 3. but your output won't fail because modals will only work if your total count of elements modal is 0, in this case 10 elements will fail, because in last loop modal statement won't work. 我发现,针对此类任务的最佳故障保护方法是将所有数据(在本例中为所有10个打印字符串)分组在数组中,而不是将此数组拆分为大块http://php.net/manual/en/function。 array-chunk.php ,然后您就可以使用这个新数组,因为它分成多个块,每个块不大于示例3。但是您的输出不会失败,因为模态仅在总计数时才起作用的元素模态为0,在这种情况下10个元素将失败,因为在最后一个循环中,模态语句将不起作用。

this will add failsafe when there isn't total elements that divides with 3. 当没有除以3的元素总数时,这将添加故障保护。

$counter = 0; 
$arr = array();
WHILE ($counter < 10) 
{ 
  $arr[] =  "counter is now " . $counter . "<br>"; 
  $counter++; 
}

$arr = array_chunk($arr, 3);
foreach ($arr as $chunk) {
    //wrap each chunk into div
    echo "<div>"
    foreach ($chunk as $string) {
        //print your string here
     }
    echo "</div>"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM