2017-04-19 85 views
0

其实我有三个foreach循环。当第三个foreach循环值($ val == $ test)匹配时,我想跳过第二个foreach循环。 这是我的代码。如何在codeigniter中匹配值时跳过foreach循环

<tbody> 
    <?php foreach ($tests as $test): ?> 
    <tr> 
    <td><?= $test; ?></td> 
    <?php foreach ($room as $key => $value): ?> 
     <?php foreach ($value['dates'] as $val) : ?> 
     <?php if ($val == $test) { ?> 
     <td><span class="text-danger"><?= $value['room_id'];?</span></td> 
     <?php } ?> 
     <?php endforeach; ?> 
    <td><span class="text-custom"><?= $value['room_id']; ?></span></td> 
    <?php endforeach; ?> 
    </tr> 
<?php endforeach; ?> 
</tbody> 
+2

@Saty不是'继续'吗? –

+0

你的意思是,如果$ val == $ test那么它应该去第一个foreach意味着$房间是一个吗? – rahulsm

+0

我会完全失去第三个循环 - 唯一的区别是类名。看看使用'in_array'这样的东西来查看'$ test ['dates']是否存在'$ test' - 如果是的话就改变类。 – Tom

回答

0

我仍然认为你不需要第三个循环。您可以删除它并用in_array()替换它,然后根据结果更改类名称。

<tbody> 
    <?php foreach ($tests as $test): ?> 
    <tr> 
     <td><?= $test; ?></td> 
     <?php 
      foreach ($room as $key => $value): 
       $class = "text-custom"; 
       if(in_array($test, $value['dates']) { 
        $class = "text-danger"; 
       } 

      <td><span class="<?php echo $class; ?>"><?= $value['room_id'];?</span></td> 

     <?php endforeach; ?> 
    </tr> 
    <?php endforeach; ?> 
</tbody> 

几个百分点 - 您可以通过使用三元if声明使这一清洁 - 使用短标签(<?= ?>)因为他们并不总是由服务器支持的时候也小心。

+0

谢谢你@@@@ thebluefox ....你解决了我的项目.....谢谢你谢谢你谢谢.. – shalder

+0

我的荣幸@沙漠 – Tom

0
<tbody> 
<?php foreach ($tests as $test): ?> 
    <tr> 
     <td><?= $test; ?></td> 
     <?php 
     $bool = FALSE; 
     foreach ($room as $key => $value): 
      ?> 
      <?php foreach ($value['dates'] as $val) : ?> 
       <?php if ($val == $test) { ?> 
        <td><span class="text-danger"><?= $value['room_id']; ?></span></td> 
        <?php 
        $bool = TRUE; 
        break; 
       } 
       ?> 
      <?php endforeach; ?> 
      <td><span class="text-custom"><?= $value['room_id']; ?></span></td> 

      <?php 
      if ($bool) { 
       continue; 
      } 

     endforeach; 
     ?> 
    </tr> 
    <?php 
endforeach; 
?> 

使用此代码,我申请休息;在第二个循环的基础上的第三个循环条件。希望这会为你工作

+0

实际上它不适合我工作..感谢您的帮助 – shalder

+0

现在使用我替换中断继续 –

0

你可以使用continue。如果你想跳过嵌套循环,那么你可以写continuen,其中n是你想跳过的嵌套循环的数量。

continue将跳过迭代。

break将结束循环。