我有一个数组,它存储了我希望使用的几个整数 table 和 for 循环遍历数组因此,代码的详细信息可能如下所示:
for($i=0;$i<sizeof($array);$i++){
<table>
<tr><td>$array[$i]</td></tr>
</table>
但是,我想添加一个功能:每个表格行指定为 2 列,
最后结果应该如下。
34 23
11 10
29 10 ......等等。
请帮助我,非常感谢:)
我的建议:
<?php
function printRows($arr) {
print '<tr>';
for($i = 0; $i < count($arr); $i++) {
print '<td>' . $arr[$i] . '</td>';
if($i%2!=0&&$i!=count($arr)-1) print '</tr><tr>';
}
print '</tr>';
}
$arr = array(1,2,3,4,5,6);
?>
用法:
<table>
<?php printRows($arr); ?>
</table>
<table>
<?php
$size = sizeof($array); // Store the size to reduce computations
for($i=0;$i<$size;$i+=2) // Loop through the array
{
echo("<tr>"); // Print row begin tag
echo("<td>".$array[$i]."</td>"); // Print out first column value
if ($i + 1 < $size) // If we can, print out the second column value
echo("<td>".$array[$i+1]."</td>");
echo("</tr>"); // Print row end tag
}
?>
</table>