我使用3个php数组来创建表,这三个数组用于生成表头。
$array1 = array('A','B');
$array2 = array('S','C');
$array3 = array('A1','B1','C1','D1');
生成一个包含这些数组值的表格。
我想使用这三个数组创建一个像图像一样的表。
答案 0 :(得分:1)
以下代码用于动态生成您显示的表。您可以分别更改$ array1,$ array2或$ array3的值,并动态生成表。
<?php
$array1 = array('A','B','C');
$array2 = array('S','C','M');
$array3 = array('A1','B1','C1','D1','E1','F1');
/* Get the length of all arrays */
$lengthArray1 = count($array1);
$lengthArray2 = count($array2);
$lengthArray3 = count($array3);
?>
<table border="1" width="50%">
<thead>
<!-- Print the header -->
<tr>
<!-- For first empty space -->
<td></td>
<?php
/* Based on the length of the array display the headers */
for($i = 0; $i < $lengthArray1; $i++){ ?>
<!-- Align center and add the colspan wrt to $array length -->
<td colspan="<?php echo $lengthArray2; ?>" align="center">
<?php echo $array1[$i]; ?>
</td>
<?php
} ?>
</tr>
<tr>
<!-- For first empty space -->
<td></td>
<?php
/* Loop through all the $array1 so that that many $array2 values will be assigned to $array1 header */
for($i = 0; $i < $lengthArray1; $i++){
for($j = 0; $j < $lengthArray2; $j++){
?>
<td align="center">
<?php echo $array2[$j]; ?>
</td>
<?php
}
} ?>
</tr>
</thead>
<tbody>
<!-- Loop through $array3 -->
<?php
for($i = 0; $i < $lengthArray3; $i++){ ?>
<tr>
<!-- Print $array3 value in first td -->
<td><?php echo $array3[$i]; ?></td>
<?php
/* To get the remaining td's I have multiplied $array1 X $array2 */
for($j = 0; $j < ($lengthArray1 * $lengthArray2); $j++){
?>
<td></td>
<?php
}
?>
</tr>
<?php
} ?>
</tbody>
</table>