gpt4 book ai didi

php - 仅回显 foreach 中的内容一次

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:05:47 24 4
gpt4 key购买 nike

我正在尝试回显 foreach 中的内容一次。目前,当用户填写表格时,会为每条跳过的记录显示消息。如果有 35 条记录被跳过,我将收到 35 条消息,因为 foreach。我想避免这种情况,并且能够为整个结果页面只显示一个回显。我怎样才能做到这一点?我想我可能必须在 foreach 之外执行此操作,但我不知道如何将其从 foreach 中取出。

foreach($allcourses as $course)
{
if(Auth::LoggedIn())
{
if(Auth::$userinfo->rank == 'Student')
{
if($course->aircraft == '1')
{
echo '<div class="msg-red">Some lessons could not be found, because you may not be entitled to view/book them at this stage of your course.</div><br/>';
continue;
}
if($course->aircraft == '2')
{
echo '<div class="msg-red">Some lessons could not be found, because you may not be entitled to view/book them at this stage of your course.</div><br/>';
continue;
}
}
}
}

最佳答案

假设您必须维护该对象的结构,如果 $course->aircraft == 1 然后相应地回显,您可以只进行 bool 值更新:

$found = false;
foreach($allcourses as $course)
{
if(Auth::LoggedIn())
{
if(Auth::$userinfo->rank == 'Student')
{

if($course->aircraft == '1')
{
$found = true;
}
}
}
}
if($found)
{
echo '<div class="msg-red">Some lessons could not be found, because you may not be entitled to view/book them at this stage of your course.</div><br/>';
}

关于php - 仅回显 foreach 中的内容一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16867514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com