php - Laravel:当值与上一个值不同时如何移动新行,刀片中的foreach
问题描述
在进行循环过程时,我想比较当前值与先前值不同时,然后移动新行。
--------------------------
|unique_id| name | stats |
--------------------------
|1 | A | P |
|1 | B | P |
|1 | C | P |
|2 | D | P |
--------------------------
这是我在刀片中的代码:
@foreach ($result as $item)
{{ $item->stats}}
@endforeach
解决方案
定义$previousstatus
外部foreach
。运行后foreach
,该变量将设置 current 的值,status
这将在下一次运行中对您有所帮助。
@php $previousstatus = ''; @endphp
@foreach ($result as $item)
//@if($previousstatus == $item->stats) @endif do as you want with previousstatus
{{ $item->stats}}
@php $previousstatus = $item->stats; @endphp //set the status for next loop becuase we need to remember in next loop what was the last element so store it in temp(previousstatus ) varible,
@endforeach
推荐阅读
- c# - 带有 Convert.ToInt32 的奇怪 FormatException
- android - 为什么 jni 调用在修改可组合记忆值时失败?
- c# - 如何检查引用的 Razor 类库中是否存在视图?
- c - switch下if语句内的计算
- sql - 查询名称附加日期模式的多个表
- vue.js - Nuxt:客户端 API 请求中的错误 Axios 基本 URL
- unity3d - GetKeyDown 总是返回真正的结果堆栈溢出
- c++ - 后缀程序的中缀没有产生正确的输出
- javascript - TypeError:无法在“Node”上执行“appendChild”:参数 1 不是“Node”类型。我做错什么了吗?
- javascript - ffmpeg 错误:无法从 javascript 中的视频制作缩略图