如何从PHP数组创建HTML表格?标题为'标题','价格'和'号'的表格。如何从PHP数组创建HTML表?
$shop = array(
array("rose", 1.25, 15),
array("daisy", 0.75, 25),
array("orchid", 1.15, 7),
);
如何从PHP数组创建HTML表格?标题为'标题','价格'和'号'的表格。如何从PHP数组创建HTML表?
$shop = array(
array("rose", 1.25, 15),
array("daisy", 0.75, 25),
array("orchid", 1.15, 7),
);
构建两个foreach循环并迭代你的数组。 打印出该值并在其周围添加HTML表格标签。
<table>
<tr>
<td>title</td>
<td>price</td>
<td>number</td>
</tr>
<? foreach ($shop as $row) : ?>
<tr>
<td><? echo $row[0]; ?></td>
<td><? echo $row[1]; ?></td>
<td><? echo $row[2]; ?></td>
</tr>
<? endforeach; ?>
</table>
什么是模板标签的问题? @HalfCrazed – 2014-10-06 07:24:53
短标签不适用于所有系统 – DarkMukke 2015-07-31 14:39:36
<?php
echo "<table>
<tr>
<th>title</th>
<th>price</th>
<th>number</th>
</tr>";
for ($i=0; $i<count($shop, 0); $i++)
{
echo '<tr>';
for ($j=0; $j<3; $j++)
{
echo '<td>'.$shop[$i][$j].'</td>';
}
echo '</tr>';
}
echo '</table>';
您可以优化它,但应该做的。
这将是最好只把数据放到数组是这样的:
<?php
$shop = array(array("title"=>"rose", "price"=>1.25 , "number"=>15),
array("title"=>"daisy", "price"=>0.75 , "number"=>25),
array("title"=>"orchid", "price"=>1.15 , "number"=>7)
);
?>
然后做这样的事情,这应该很好地工作,甚至当你在数据库中添加更多的列到你的桌子后面。
<?php if (count($shop) > 0): ?>
<table>
<thead>
<tr>
<th><?php echo implode('</th><th>', array_keys(current($shop))); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($shop as $row): array_map('htmlentities', $row); ?>
<tr>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
echo '<table><tr><th>Title</th><th>Price</th><th>Number</th></tr>';
foreach($shop as $id => $item) {
echo '<tr><td>'.$item[0].'</td><td>'.$item[1].'</td><td>'.$item[2].'</td></tr>';
}
echo '</table>';
您可以使用foreach
遍历数组$shop
,并得到一个阵列,每个迭代呼应这样的价值观:
echo '<table>';
echo '<tr><th>title</td><td>price</td><td>number</td></tr>';
foreach ($shop as $item) {
echo '<tr>';
echo '<td>'.$item[0].'</td>';
echo '<td>'.$item[1].'</td>';
echo '<td>'.$item[2].'</td>';
ehoc '</tr>';
}
echo '</table>';
<table>
<thead>
<tr><th>title</th><th>price><th>number</th></tr>
</thead>
<tbody>
<?php
foreach ($shop as $row) {
echo '<tr>';
foreach ($row as $item) {
echo "<td>{$item}</td>";
}
echo '</tr>';
}
?>
</tbody>
</table>
echo "<table><tr><th>Title</th><th>Price</th><th>Number</th></tr>";
foreach($shop as $v){
echo "<tr>";
foreach($v as $vv){
echo "<td>{$vv}</td>";
}
echo "<tr>";
}
echo "</table>";
这里我的答案。
function array2Html($array, $table = true)
{
$out = '';
foreach ($array as $key => $value) {
if (is_array($value)) {
if (!isset($tableHeader)) {
$tableHeader =
'<th>' .
implode('</th><th>', array_keys($value)) .
'</th>';
}
array_keys($value);
$out .= '<tr>';
$out .= array2Html($value, false);
$out .= '</tr>';
} else {
$out .= "<td>$value</td>";
}
}
if ($table) {
return '<table>' . $tableHeader . $out . '</table>';
} else {
return $out;
}
}
但是,您的表头必须是数组的一部分,当它来自数据库时这很常见。例如
$shop = array(
array(
'title' => 'rose',
'price' => 1.25,
'number' => 15,
),
array(
'title' => 'daisy',
'price' => 0.75,
'number' => 25,
),
array(
'title' => 'orchid',
'price' => 1.15,
'number' => 7,
),
);
print arrayToHtml($shop);
希望它帮助;)
这里是我的:
<?php
function build_table($array){
// start table
$html = '<table>';
// header row
$html .= '<tr>';
foreach($array[0] as $key=>$value){
$html .= '<th>' . htmlspecialchars($key) . '</th>';
}
$html .= '</tr>';
// data rows
foreach($array as $key=>$value){
$html .= '<tr>';
foreach($value as $key2=>$value2){
$html .= '<td>' . htmlspecialchars($value2) . '</td>';
}
$html .= '</tr>';
}
// finish table and return it
$html .= '</table>';
return $html;
}
$array = array(
array('first'=>'tom', 'last'=>'smith', 'email'=>'[email protected]', 'company'=>'example ltd'),
array('first'=>'hugh', 'last'=>'blogs', 'email'=>'[email protected]', 'company'=>'example ltd'),
array('first'=>'steph', 'last'=>'brown', 'email'=>'[email protected]', 'company'=>'example ltd')
);
echo build_table($array);
?>
您可以使用此功能。
function insertTable($myTableArrayBody) {
$x = 0;
$y = 0;
$seTableStr = '<table><tbody>';
while (isset($myTableArrayBody[$y][$x])) {
$seTableStr .= '<tr>';
while (isset($myTableArrayBody[$y][$x])) {
$seTableStr .= '<td>' . $myTableArrayBody[$y][$x] . '</td>';
$x++;
}
$seTableStr .= '</tr>';
$x = 0;
$y++;
}
$seTableStr .= '</tbody></table>';
return $seTableStr;
}
<table id="usuarios">
<tbody>
<tr>
<th>Nombre</th>
<th>Apellido</th>
<th>Email</th>
<th>Institución educativa</th>
<th>Fecha Registro</th>
</tr>
<?php
if ($result->num_rows > 0) {// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["Nombre"]."</td>";
echo "<td>".$row["Apellido"]."</td>";
echo "<td>".$row["Email"]."</td>";
echo "<td>".$row["NombreInsEdu"]."</td>";
echo "<td>".$row["FechaRegistro"]."</td></tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</tbody>
</table>
您也可以还使用array_reduce
例如:
在身体的前面加上表头,您可以设置第二个参数$myTableArrayHeader
和做相同的报头信息
$tbody = array_reduce($rows, function($a, $b){return $a.="<tr><td>".implode("</td><td>",$b)."</td></tr>";});
$thead = "<tr><th>" . implode("</th><th>", array_keys($rows[0])) . "</th></tr>";
echo "<table>\n$thead\n$tbody\n</table>";
PHP代码:
$multiarray = array (
array("name"=>"Argishti", "surname"=>"Yeghiazaryan"),
array("name"=>"Armen", "surname"=>"Mkhitaryan"),
array("name"=>"Arshak", "surname"=>"Aghabekyan"),
);
$count = 0;
foreach ($multiarray as $arrays){
$count++;
echo "<table>" ;
echo "<span>table $count</span>";
echo "<tr>";
foreach ($arrays as $names => $surnames){
echo "<th>$names</th>";
echo "<td>$surnames</td>";
}
echo "</tr>";
echo "</table>";
}
CSS:
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;``
}
数组表。 数组转换为div。 JSON到表中。 将JSON转换为div。
所有的都很好地处理这个类。 Click here to get a class
如何使用它?
刚刚获得和对象
$obj = new Arrayinto();
创建要转换
$obj->array_object = array("AAA" => "1111",
"BBB" => "2222",
"CCC" => array("CCC-1" => "123",
"CCC-2" => array("CCC-2222-A" => "CA2",
"CCC-2222=B" => "CB2"
)
)
);
如果你想数组转换为表数组。调用这个。
$result = $obj->process_table();
如果您想将数组转换为div。调用这个。
$result = $obj->process_div();
我们假设,如果你有一个JSON
$obj->json_string = '{
"AAA":"11111",
"BBB":"22222",
"CCC":[
{
"CCC-1":"123"
},
{
"CCC-2":"456"
}
]
}
';
可以转换成表格/ DIV这样
$result = $obj->process_json('div');
OR
$result = $obj->process_json('table');
希望this'd帮助您。
这是完全相同的问题http://stackoverflow.com/questions/4746079/how-do-i-create-table-with-this-php-array请不要双重发布问题 - 你只是浪费你自己(和其他人)的时间。 – 2011-01-20 18:15:58
合并。并且,祈祷我不再合并! – Will 2011-01-20 18:40:01
此线程已在这里回答[点击此处](https://stackoverflow.com/questions/4746079/how-to-create-a-html-table-from-a-php-array/47068295#47068295) – Delickate 2017-11-02 06:08:56