我想使用php和值数组创建一个包含rowspan的表,但我真的很难创建以下内容:
<table>
<thead>
<tr>
<th>Month</th>
<th>Tests</th>
<th>Values</th>
<th>Month</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2">January</td>
<td>Test A</td>
<td>VAL A</td>
<td rowspan="2">FEB</td>
</tr>
<tr>
<td>Test B</td>
<td>VAL B</td>
</tr>
</tbody>
</table>
&#13;
使用以下值的php数组我用 print_r()函数来显示数组如何使用php使用这个值数组来创建上面的表格式
Array ( [0] => Array ( [month] => January [tests] => Test A,Test B [values] => VAL A,VAL B [month2] => Feb ) )
答案 0 :(得分:0)
试用此代码:
<?php //The Array
$arr=array(array(
'month' => "January",
'month2' => 'Feb',
'tests' => "Test A, Test B",
'values' => "VAL A, VAL B"
));
?>
<table>
<thead>
<tr>
<th>Month</th>
<th>Tests</th>
<th>Values</th>
<th>Month</th>
</tr>
</thead>
<tbody>
<?php foreach($arr as $item): ?>
<?php
$test_break=explode(',',$item['tests']);
$values_break=explode(',',$item['values']);
?>
<tr>
<td rowspan="2"><?php echo $item["month"] ?></td>
<td><?php echo $test_break[0]; ?></td>
<td><?php echo $values_break[0]; ?></td>
<td rowspan="2"><?php echo $item["month2"] ?></td>
</tr>
<tr>
<td><?php echo $test_break[1]; ?></td>
<td><?php echo $values_break[1]; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>