简体   繁体   English

在PHP中,仅在Foreach循环中将“ IF”设置为true后再停止,然后继续Foreach

[英]In PHP Stop only “IF” once it true in Foreach loop and Continue Foreach

How to stop IF conduction in side the loop, once it TRUE without stopping loop. 如何在回路旁停止IF导通(一旦为TRUE则不停止回路)。 I want to get HTML output as following, 我想获得如下的HTML输出,

<div1>
  Group_1
      Item_1
      Item_21
      Item_43
      Item_55
  Group_2
      Item_5
      Item_34
      Item_56
  Group_3
      Item_12
  Group_4
      Item_11
      Item_14
      Item_15
</div1>

<div2>
  Group_5
      Item_2
      Item_3
  Group_6
      Item_6
      Item_7
      Item_9
  Group_7
      Item_10
      Item_41
      Item_42
      Item_43
  Group_8
      Item_13
</div2>

<div3>
  Group_9
      Item_16
      Item_17
      Item_18
      Item_19
      Item_20
      Item_30
      Item_31
      Item_32
      Item_33
      Item_35
      Item_40
      Item_44
</div3>

<div4>
   Group_10
      Item_4
      Item_22
      Item_23
  Group_11
      Item_24
      Item_25
</div4>

My PHP code is as following, 我的PHP代码如下,

<?php
  $x=0;
  $num_g =  count($groups);
  $num_i =  count($items);
  $lines =  $num_g+$num_i;
  $div1 = 13;
  $div2 = 26;
  $div3 = 39;
  $div4 = 51;

  foreach($groups as $group){

    if($x==0){ 
      echo'<div class="div1">'; // Start First DIV
    }
      $x++; 

    echo 'Group - '.$group->name.'<br>'; // Group Name
    foreach($items as $item){
      if($item->group_id == $group->id) //Group Items
          $x++; 
          echo $item->name.'<br>';  // Item Name
      }


    if($x>=$div1){  // Need to stop Only this If once it TRUE ??
        echo '</div><div class="div2">'; // Start 2nd DIV
    }

    if($x>=$div2){   // Need to stop Only this If once it TRUE ??
        echo '</div><div class="div3">'; // Start 3rd DIV
    }

    if($x>=$div3){  // Need to stop Only this If once it TRUE ??
        echo '</div><div class="div4">'; // Start 4th DIV
    }

    if($x==$lines) {   // Need to stop Only this If once it TRUE ??
        echo '</div>'; // End Last DIV
    }
  }

Please, Can anyone help or advice about this matter ? 请,有人可以对此事提供帮助或建议吗?

Use the continue keyword. 使用continue关键字。 the official PHp documentation says: 官方的PHp 文档说:

continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration. 在循环结构中使用continue可以跳过当前循环迭代的其余部分,并在条件评估和下一个迭代的开始处继续执行。

use continue statement in the place you want to skip the remain code in the foreach. 在要跳过foreach中的剩余代码的地方使用continue语句。 refer to the manual 请参阅手册

You need to change your code: 您需要更改代码:

    if($x==$lines) {   // Need to stop Only this If once it TRUE ??
        echo '</div>'; // End Last DIV
        break;
    }
    if($x>=$div3){  // Need to stop Only this If once it TRUE ??
        echo '</div><div class="div4">'; // Start 4th DIV
        continue;
    }
    if($x>=$div2){   // Need to stop Only this If once it TRUE ??
        echo '</div><div class="div3">'; // Start 3rd DIV
        continue;
    }
    if($x>=$div1){  // Need to stop Only this If once it TRUE ??
        echo '</div><div class="div2">'; // Start 2nd DIV
    }

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

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