php 如何将数组转换为html表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4529965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert an array to a html table
提问by jim
Can someone help me with this array that I have? I want to create a table that is a maximum of 5 columns and a maximum of 15 rows. If there are only 4 rows, for example, then only 4 rows should be shown instead of the 15. If there are only 3 cells that have data then the the remaining 2 should be padded with $nbsp;
.
有人可以帮我处理我拥有的这个阵列吗?我想创建一个最多 5 列和最多 15 行的表。例如,如果只有 4 行,那么应该只显示 4 行而不是 15 行。如果只有 3 个单元格有数据,那么剩下的 2 行应该用 填充$nbsp;
。
Here is my sample array:
这是我的示例数组:
Array
(
[0] => Array
(
[name] => test1
[item_id] => 1
)
[1] => Array
(
[name] => test2
[item_id] => 2
)
[2] => Array
(
[name] => test3
[item_id] => 3
)
[3] => Array
(
[name] => test4
[item_id] => 4
)
[4] => Array
(
[name] => test5
[item_id] => 5
)
[5] => Array
(
[name] => test6
[item_id] => 6
)
)
My data repeats if there is a new row added. This is my issue at present.
如果添加了新行,我的数据会重复。这是我目前的问题。
$row = count( $array ) / 5;
$col = 5;
echo'<table border="1" width="700">';
for( $i = 0; $i < $row; $i++ )
{
echo'<tr>';
for( $j = 0; $j < $col; $j++ ) {
if( ! empty( $array[$j] ) ) {
echo '<td>'.$array[$j]['item_id'].'</td>';
}
}
echo'</tr>';
}
echo'</table>';
回答by cbrandolino
Let's call your array $rows
, ok?
让我们调用你的数组$rows
,好吗?
echo "<table>";
foreach ($rows as $row) {
echo "<tr>";
foreach ($row as $column) {
echo "<td>$column</td>";
}
echo "</tr>";
}
echo "</table>";
Using foreach
is more idiomatic for looping trough arrays in php, and it greatly increase your code's readability. Plus, the only variable you need for this is one containing the array itself.
foreach
在 php 中使用循环槽数组更为惯用,它大大提高了代码的可读性。另外,您需要的唯一变量是包含数组本身的变量。
回答by Sarfraz
Check out:
查看:
It is basically simple logic something like this:
它基本上是这样的简单逻辑:
echo "<table border=\"5\" cellpadding=\"10\">";
for ($i=0; $i < count($input); $i++)
{
echo "<tr>";
for ($c=0; $c<$cols; $c++)
{
echo "<td>$input[$i]</td>";
}
echo "</tr>";
}
echo "</table>";
回答by jbrahy
Here's a snippet I wrote to test converting a php array to html.
这是我编写的一个片段,用于测试将 php 数组转换为 html。
$row = array(
'column 1' => 'value',
'column 2' => 'value',
'column 3' => 'value',
'column 4' => 'value',
'column 5' => 'value',
'column 6' => 'value',
'column 7' => 'value',
);
$rows = array($row, $row, $row, $row, $row, $row, $row, $row, $row, $row);
print array_to_html($rows);
function array_to_html($data) {
$report = "";
if (count($data) > 0) {
$report .= "<table>";
$report .= sprintf("<tr><th>%s</th></tr>", join("</th><th>", array_keys($data[0])));
foreach ($data as $row) {
$report .= "<tr>";
foreach ($row as $column) {
$report .= "<td>$column</td>";
}
$report .= "</tr>";
}
$report .= "</table>";
} else {
$report = "No data";
}
return $report;
}