在PHP中将多维关联数组显示为表格 [英] Displaying an Multidimensional associative array as a table in PHP
本文介绍了在PHP中将多维关联数组显示为表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在研究PHP中的关联数组.在这里我创建了一个多关联数组.我想以表格形式显示我的数组.数组是
I was working on associative arrays in PHP. Here I made a multiple associative array.I want to show my array in tabular form. The array is
<?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"
)
),
);
我尝试显示数据的方式如下:
The way I tried to display the data is below:
<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>
但是我没有在正确的行和列中得到结果.因此,我应该怎么做才能以精确的表格形式显示它,因为我从未经历过此类任务.
But I do not get the result in proper rows and columns. So what shall I do to display it in an exact tabular form because I have never undergone from this sort of task.
推荐答案
foreach($schoolDetails as $schoolDetail) {
echo "<tr>";
echo "<td>{$schoolDetail[0]["schoolName"]}</td>";
echo "<td>{$schoolDetail[1]["branchName"]}</td>";
echo "<td>{$schoolDetail[1]["phone"]}</td>";
echo "<td>{$schoolDetail[1]["numberofTeachers"]}</td>";
if(!empty($schoolDetail[3]['departmentName'])) {
echo "<td>{$schoolDetail[2]["departmentName"]}" . ", " . "{$schoolDetail[3]["departmentName"]}</td>";
} else {
echo "<td>{$schoolDetail[2]["departmentName"]}</td>";
}
if(!empty($schoolDetail[3]['HOD'])) {
echo "<td>{$schoolDetail[2]["HOD"]}" . ", " . "{$schoolDetail[3]["HOD"]}</td>";
} else {
echo "<td>{$schoolDetail[2]["HOD"]}</td>";
}
echo "</tr>";
}
这篇关于在PHP中将多维关联数组显示为表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文