php - 在 laravel 中使用数组作为 html 表
问题描述
尝试获取我的数组并将值插入 html 表结构时,我的刀片上出现未定义的索引错误。
我有数组循环并将其发送到刀片:
控制器
$result = array();
foreach($getItem as $Item){
$result[$Item->item_id][] = $Item;
}
//returning to blade but not included here
转储数组:
array:26 [▼
11873 => array:2 [▼
0 => {#407 ▼
+"item_id": "11873"
+"item_name": "Title"
+"item_comment": "Item Title"
+"item_type": "2"
}
1 => {#408 ▼
+"item_id": "11873"
+"item_name": "Instruction"
+"item_comment": "Inst Comment"
+"item_type": "2"
}
]
刀:
@foreach ($result as $id => $item)
<tr>
<td>{{ $item['item_id'] }}</td>
@if($item['item_name'] == "Title")
<td>{{ $item['item_comment'] }}</td>
@endif
<td>{{ $item['item_type'] }}</td>
</tr>
@endforeach
因此,对于转储数组结构,一个问题是“item_type”应该与 id 处于较高级别,而不应该在每个嵌套级别中。但除此之外,我得到未定义的索引错误。我只是循环不正确吗?
解决方案
In @foreach ($result as $id => $item)
,$item
是一个数组,您还需要遍历它。
@foreach ($result as $id => $item)
<tr>
<td>{{ $id }}</td>
@foreach($item as $subitem)
@if($subitem['item_name'] == "Title")
<td>{{ $subitem['item_comment'] }}</td>
@endif
<td>{{ $subitem['item_type'] }}</td>
@endforeach
</tr>
@endforeach
推荐阅读
- java - Jackson:将空或 null HashMap 序列化为空数组
- python-3.x - 无法在 python 3 上将复杂转换为浮点数
- spring - 从 Controller 发出的多个事务调用
- node.js - Mongo db Atlas 3.6.5 身份验证失败
- ios - 如何检测用户是否关闭了特定 iOS 应用的移动数据
- sed - 用于删除换行符的 SED 代码
- windows - 适用于 Windows Server 2016 的 Oracle 驱动程序
- php - 更新加入功能在 php 中无法正常工作
- mqtt - 将 MQTT 消息发送到 Orion 上下文代理
- mongodb - MongoDB:如何查找数据库中集合之间的关系