【发布时间】:2021-01-10 18:52:30
【问题描述】:
我正在研究 PHP 中的关联数组。在这里,我制作了一个多重关联数组。我想以表格形式显示我的数组。数组是
<?php
$schoolDetails = array(
//First School Details
array(
array("schoolName" => "TCS"),
array(
"branchName" => "Iqbal Campus",
"phone" => "052-4667281",
"numberofTeachers" => 51
),
array(
"departmentName" => "Maths",
"HOD" => "Mr Ali"
),
array(
"departmentName" => "Science",
"HOD" => "Mr Imran"
)
),
array(
array("schoolName" => "CSS"),
array(
"branchName" => "Gohdpur Campus",
"phone" => "052-4667281",
"numberofTeachers" => 20
),
array(
"departmentName" => "Chemistry",
"HOD" => "Mr Abdullah"
),
array(
"departmentName" => "Computer",
"HOD" => "Mr Naeem"
)
),
);
我尝试显示数据的方式如下:
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>School Name</th>
<th>Branch Name</th>
<th>Phone Number</th>
<th>Number of Teachers</th>
<th>Department Name</th>
<th>HOD</th>
</tr>
</thead>
<tfoot>
<tr>
<th>School Name</th>
<th>Branch Name</th>
<th>Phone Number</th>
<th>Number of Teachers</th>
<th>Department Name</th>
<th>HOD</th>
</tr>
</tfoot>
<tbody>
<?php foreach ($schoolDetails as $school) {
foreach ($school as $schools) {
foreach ($schools as $key => $value) {?>
<tr>
<td><?php echo $value; ?></td>
</tr>
<?php }}}?>
</tbody>
</table>
但我没有在正确的行和列中得到结果。那么我应该怎么做才能以精确的表格形式显示它,因为我从未经历过这种任务。
【问题讨论】:
标签: php arrays multidimensional-array html-table associative-array