我有变量$count
其中包含我的数组的总元素。
我想将 html 代码插入到另一个变量,该变量包含与变量 $count 的行数一样多的表。
我该怎么办?
<?php
$count=5;
$html="<table>
<tr>
<td></td>
<td></td>
</tr>
</table>"
?>
最佳答案
有很多可能性可以做到这一点。
使用循环:
$table = '<table>';
for ($i = 0; $i < $count; $i++) {
$table .= '<tr><td></td><td></td></tr>';
}
$table .= '</table>';
或者你可以使用str_repeat
$table = '<table>';
$table .= str_repeat('<tr><td></td><td></td></tr>', $count);
$table .= '</table>';
或许多其他 - 取决于您的需求
关于javascript - 从数组的数字元素创建 HTML 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33396399/