如何在codeigniter中匹配值时跳过foreach循环
其实我有三个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>
我仍然认为你不需要第三个循环。您可以删除它并用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
声明使这一清洁 - 使用短标签(<?= ?>
)因为他们并不总是由服务器支持的时候也小心。
<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;
?>
使用此代码,我申请休息;在第二个循环的基础上的第三个循环条件。希望这会为你工作
实际上它不适合我工作..感谢您的帮助 – shalder
现在使用我替换中断继续 –
你可以使用continue
。如果你想跳过嵌套循环,那么你可以写continue
n
,其中n是你想跳过的嵌套循环的数量。
continue
将跳过迭代。
break
将结束循环。
@Saty不是'继续'吗? –
你的意思是,如果$ val == $ test那么它应该去第一个foreach意味着$房间是一个吗? – rahulsm
我会完全失去第三个循环 - 唯一的区别是类名。看看使用'in_array'这样的东西来查看'$ test ['dates']是否存在'$ test' - 如果是的话就改变类。 – Tom