我有一个如下数组:
array(2) {
[0]=> string(15) "University Core"
[1]=> array(4) {
[0]=> array(3) {
[1]=> string(8) "Language"
[3]=> string(2) "12"
[2]=> array(5) { [0]=> string(6) "ENG102" [1]=> string(6) "ENG111" }
}
[1]=> array(3) {
[1]=> string(10) "Humanities"
[3]=> string(1) "9"
[2]=> array(4) { [0]=> string(6) "PHI104" } }
[2]=> array(3) {
[1]=> string(15) "Social Sciences"
[3]=> string(1) "9"
[2]=> array(8) { [0]=> string(6) "ECO101" [1]=> string(6) "ECO104" } }
[3]=> array(3) {
[1]=> string(7) "Science"
[3]=> string(1) "4"
[2]=> array(2) { [0]=> string(6) "BIO103" [1]=> string(7) "BIO103L" } } }
}
这将发送给查看。
应显示在何处
------------------------------------------------------------------
category | sub category | courselist
------------------------------------------------------------------
| Language |ENG102
| |-------------------------
| | ENG111
University Core |--------------------------------------------------
|Humanities |
| |PHI104
|--------------------------------------------------
我发现它非常复杂,真的不知道该怎么做。
关于如何操作的任何建议。?
搜索后,我需要使用rowspan和colspan。 但是这个数组很复杂,所以我需要帮助。
到目前为止,我所尝试的甚至都不是解决方案。 那就是为什么我没有在这里展示它。
答案 0 :(得分:0)
尝试此代码:-
<?php
$arr = array(
"University Core",
array(
array("Language", "12", array("ENG102", "ENG111")),
array("Humanities", "9", array("PHI104")),
array("Social Sciences", "9", array("ECO101", "ECO104")),
array("Science", "4", array("BIO103", "BIO103L"))
));
$count = 0;
foreach ($arr[1] as $subCat) {
$count += count($subCat[2]); // to generate total rows
}
?>
<table border="1" width="50%">
<thead>
<tr>
<th width="50%">Category</th>
<th width="20%">SubCategory</th>
<th width="15%">Course List</th>
</tr>
</thead>
<tbody>
<?php
foreach($arr[1] as $in => $subCat) {
echo ($in === 0)? '<tr><td rowspan="' . $count . '">' . $arr[0] . '</td>': '<tr>'; // get the main category
foreach($subCat as $key => $value) {
echo ($key === 0)? '<td rowspan="' . count($subCat[2]) . '">' . $subCat[$key] . '</td>': ''; // get the sub-category
if ($key === 2) {
foreach($value as $course) {
echo '<td>' . $course . '</td></tr>'; // get the course list
}
}
}
}
?>
</tbody>
</table>