[英]Creating multiple HTML table from multidimensional arrays
我正在嘗試從多維數組創建多個HTML表。 我已經建立了一個數組,但是對如何將該數組轉換為多個HTML表一無所知。
我想根據當天打開的時間和班級名稱來分開表格。
為了清楚起見,這是我的代碼:
$title = [];
$table_rows = [];
// build table arrays
foreach ($schedule AS $row)
{
$title[] = $row->time.' - '.$row->class_name;
$table_rows[$row->time][$row->class_name][] = ['student_id' => $row->student_id, 'mentor_code' => $row->mentor_code, 'student_name' => $row->student_name];
}
// $title array (using array_unique($title) to removes duplicate values
Array
(
[0] => 07:30-08:30 - E-1
[1] => 08:30-09:30 - E-1
[2] => 10:00-11:00 - E-1
[3] => 11:00-12:00 - E-1
[12] => 07:30-08:30 - E-2
[13] => 08:30-09:30 - E-2
)
// $table_rows array
Array
(
[07:30-08:30] => Array
(
[E-1] => Array
(
[0] => Array
(
[student_id] => 1012836001
[mentor_code] => TPA-1
[student_name] => Vanessa
)
[1] => Array
(
[student_id] => 1012836002
[mentor_code] => TPA-1
[student_name] => Kesya
)
[2] => Array
(
[student_id] => 3012836003
[mentor_code] => TPA-1
[student_name] => Charissa
)
)
[E-2] => Array
(
[0] => Array
(
[student_id] => 1012836004
[mentor_code] => FIS-1
[student_name] => Drex
)
[1] => Array
(
[student_id] => 3012836005
[mentor_code] => FIS-1
[student_name] => Vulcano
)
)
)
[08:30-09:30] => Array
(
[E-1] => Array
(
[0] => Array
(
[student_id] => 1012836001
[mentor_code] => TPA-1
[student_name] => Vanessa
)
[1] => Array
(
[student_id] => 1012836002
[mentor_code] => TPA-1
[student_name] => Kesya
)
[2] => Array
(
[student_id] => 3012836003
[mentor_code] => TPA-1
[student_name] => Charissa
)
)
[E-2] => Array
(
[0] => Array
(
[student_id] => 1012836004
[mentor_code] => FIS-1
[student_name] => Drex
)
[1] => Array
(
[student_id] => 3012836005
[mentor_code] => FIS-1
[student_name] => Vulcano
)
)
)
[10:00-11:00] => Array
(
[E-1] => Array
(
[0] => Array
(
[student_id] => 1012836001
[mentor_code] => FIS-1
[student_name] => Vanessa
)
[1] => Array
(
[student_id] => 1012836002
[mentor_code] => FIS-1
[student_name] => Kesya
)
[2] => Array
(
[student_id] => 3012836003
[mentor_code] => FIS-1
[student_name] => Charissa
)
)
)
[11:00-12:00] => Array
(
[E-1] => Array
(
[0] => Array
(
[student_id] => 1012836001
[mentor_code] => FIS-1
[student_name] => Vanessa
)
[1] => Array
(
[student_id] => 1012836002
[mentor_code] => FIS-1
[student_name] => Kesya
)
[2] => Array
(
[student_id] => 3012836003
[mentor_code] => FIS-1
[student_name] => Charissa
)
)
)
)
從數組中,我將創建如下表:
Time : 07:30-08:30 TPA-1 Class : E-1
|------------|--------------|------------|
|Student ID | Student Name | Status |
|------------|--------------|------------|
|1012836001 | Vanessa | Check None |
|1012836002 | Kesya | Check None |
|3012836003 | Charissa | Check None |
|------------|--------------|------------|
Time : 08:30-09:30 TPA-1 Class : E-1
|------------|--------------|------------|
|Student ID | Student Name | Status |
|------------|--------------|------------|
|1012836001 | Vanessa | Check None |
|1012836002 | Kesya | Check None |
|3012836003 | Charissa | Check None |
|------------|--------------|------------|
Time : 07:30-08:30 FIS-1 Class : E-2
|------------|--------------|------------|
|Student ID | Student Name | Status |
|------------|--------------|------------|
|1012836004 | Drex | Check None |
|3012836005 | Vulcano | Check None |
|------------|--------------|------------|
Time : 08:30-09:30 FIS-1 Class : E-2
|------------|--------------|------------|
|Student ID | Student Name | Status |
|------------|--------------|------------|
|1012836004 | Drex | Check None |
|3012836005 | Vulcano | Check None |
|------------|--------------|------------|
Time : 10:00-11:00 FIS-1 Class : E-1
|------------|--------------|------------|
|Student ID | Student Name | Status |
|------------|--------------|------------|
|1012836001 | Vanessa | Check None |
|1012836002 | Kesya | Check None |
|3012836003 | Charissa | Check None |
|------------|--------------|------------|
Time : 11:00-12:00 FIS-1 Class : E-1
|------------|--------------|------------|
|Student ID | Student Name | Status |
|------------|--------------|------------|
|1012836001 | Vanessa | Check None |
|1012836002 | Kesya | Check None |
|3012836003 | Charissa | Check None |
|------------|--------------|------------|
我可以根據陣列產生結果嗎? 我真的需要幫助 如果有人可以將我指出正確的方向,將不勝感激。 謝謝。
您可以通過減少數組的多維性來簡化此過程。
將計划行按完整標題分組:
foreach ($schedule as $row)
{
$title = "Time : $row->time $row->mentor_code Class : $row->class_name";
$tables[$title][] = $row;
}
然后,您將獲得一個按標題索引的表數組。 從這一點來看,輸出每個表要簡單得多。
foreach ($tables as $title => $table) {
echo $title;
echo '<table>
<thead>
<tr>
<th>Student ID</th>
<th>Student Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>';
foreach ($table as $row) {
echo "<tr>
<td>{$row->student_id}</td>
<td>{$row->student_name}</td>
<td>Check None</td>
</tr>"
}
echo '</tbody>
</table>';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.