我需要将我的数据分组到视图Laravel中
大家好,我使用foreach在Laravel中的输出如下,我需要将我的数据分组到视图Laravel中,laravel,laravel-4,Laravel,Laravel 4,大家好,我使用foreach在Laravel中的输出如下, ItemId:3 ItemQuantity:3 ItemColor:White ItemId:3 ItemQuantity:3 ItemColor:Black ItemId:4 ItemQuantity:3 ItemColor:White 但我需要的是像这样组合具有相同ID的项目, ItemID:3 ItemQuantity:3 ItemColor:白色/ItemQuantity:3 ItemColor:黑色 ItemId:4 Item
ItemId:3 ItemQuantity:3 ItemColor:White
ItemId:3 ItemQuantity:3 ItemColor:Black
ItemId:4 ItemQuantity:3 ItemColor:White 但我需要的是像这样组合具有相同ID的项目,
ItemID:3 ItemQuantity:3 ItemColor:白色/ItemQuantity:3 ItemColor:黑色
ItemId:4 ItemQuantity:3 ItemColor:White
顺便说一句,我在视图中使用Table和Foreach来显示数据 谢谢你们。。这是Laravel 4.2
@foreach($items作为$item1)
{
"id": "1",
"item_quantity": "3",
"item_id": "3",
"item_color": "White",
},
{
"id": "2",
"item_quantity": "3",
"item_id": "3",
"item_color": "Black",
},
{
"id": "3",
"item_quantity": "3",
"item_id": "4",
"item_color": "White",
},
{{$item1->id}
{{$item1->item_数量}
{{$item1->item_color}
@foreach($items作为$item2)
@如果($item1->item\u id==$item2->item\u id)
{{$item2->item_quantity}
{{$item2->item_color}
@恩迪夫
@endforeach
@endforeach
我不确定您的输入对象是什么,但类似以下内容:
@foreach($items as $item1)
<tr>{{$item1->id}}</td>
<td>{{$item1->item_quantity}}</td>
<td>{{$item1->item_color}}</td>
@foreach($items as $item2)
@if($item1->item_id == $item2->item_id)
<td>{{$item2->item_quantity}}</td>
<td>{{$item2->item_color}}</td>
@endif
@endforeach
</tr>
@endforeach
试着更清楚地回答你的问题。这是与物料表相关的库存表吗?
function group_items($items){
$groupedOutput = array();
for each($items as $item){
$key = 'id_'.$item;
if(!array_key_exists($key, $groupedOutput)){
$groupedOutput[$key] = array();
}
$groupedOutput[$key][] = $item;
}
return $groupedOutput;
}
function print_items($groupedOutput){
for each($groupedOutput as $items){
for each($items as $item){
$itemId = $item['item_id'];
$itemQuantity = $item['item_quantity'];
$itemColor = $item['item_color'];
echo "ItemID:$itemId ItemQuantity:$itemQuantity ItemColor:$itemColor/ ";
}
}
}
//To group output
$groupedOutput = group_items($items);
//To print grouped output
print_items($groupedOutput);
// OR
var_dump(json_encode($groupedOutput));