嗨,大家好,我有一個頁面,我使用foreach
從models
開始循環顯示數據。我想知道如何分組這些類並相應地顯示。如何在yii2中對模型數組進行分組
例如我有登記表格在頁面上有日期和類。 所以我想按日期先將它們分組,然後再按classes.My registration
模型具有像下面的字段。
Registration
id
event_id
dates
class_id
user_id
title
description
現就本視圖文件我傳遞的$modelRegistration
陣列,所以我實際上不具有簡單的多維陣列,所以我不能使用該邏輯組和display.Here是我的視圖代碼以顯示forms
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Date</th>
<th>Class</th>
<th>User Name</th>
<th>Title</th>
<th>Description</th>
</tr>
</thead>
<tbody class="container-items">
<?php foreach ($modelsRegistration as $indexRegistration => $modelRegistration): ?>
<tr value="<?= $modelRegistration->date ?>" class="house-item">
<td class="col-lg-1 col-md-1 col-sm-1 col-xs-1"><?= $modelRegistration->class->name ?></td>
<td class="col-lg-2 col-md-2 col-sm-2 col-xs-2">
<label><?= $modelRegistration->user->name ?></label>
<td class="col-lg-2 col-md-2 col-sm-2 col-xs-2"><?= $modelRegistration->title ?> </td>
<td class="col-lg-1 col-md-1 col-sm-1 col-xs-1"><?= $modelClass->description ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
我想數組像下面
Registration
(
[0] => 10/12/2015
(
[0] => class-1(
[0] => array(
[title] => "Title 1"
[id] => 5
[username] => John
[Description] => Team Leader
)
[1] => array(
[title] => "Title 2"
[id] => 6
[username] => John 1
[Description] => Team 2
)
)
[1] => class-2(
[0] => array(
[title] => "Title 4"
[id] => 7
[username] => John 4
[Description] => Team 4
)
[1] => array(
[title] => "Title 5"
[id] => 9
[username] => John 5
[Description] => Team 6
)
)
[2] => class-3(
[0] => array(
[title] => "Title 1"
[id] => 5
[username] => John
[Description] => Team Leader
)
[1] => array(
[title] => "Title 2"
[id] => 6
[username] => John 1
[Description] => Team 2
)
)
)
[0] => 12/12/2015
(
[0] => class-1(
[0] => array(
[title] => "Title 8"
[id] => 12
[username] => John 10
[Description] => Team 77
)
[1] => array(
[title] => "Title 27"
[id] => 67
[username] => John 17
[Description] => Team 27
)
)
[1] => class-3(
[0] => array(
[title] => "Title 41"
[id] => 71
[username] => John 41
[Description] => Team 41
)
[1] => array(
[title] => "Title 51"
[id] => 59
[username] => John 54
[Description] => Team 64
)
)
)
如果我可以按照日期先分組,然後再按班分類,比我只想顯示日期的一次,並且在該日期之下,所有分組班級和班級下我都可以顯示其他信息。謝謝
你可以發表你想要的數組的例子嗎? – arogachev
@arogachev我已更新問題 –
如何確定日期和班級? – arogachev