如何从PHP数组创建HTML表?

ktm*_*ktm 34 html php arrays

如何从PHP数组创建HTML表?标题为"标题","价格""数字"的表格.

$shop = array(
    array("rose",   1.25, 15),
    array("daisy",  0.75, 25),
    array("orchid", 1.15, 7 ),
); 
Run Code Online (Sandbox Code Playgroud)

Ric*_*nop 72

最好将数据提取到数组中,如下所示:

<?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) 
             ); 
?>
Run Code Online (Sandbox Code Playgroud)

然后执行类似这样的操作,即使您稍后在数据库中向表中添加更多列,也应该可以正常工作.

<?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; ?>
Run Code Online (Sandbox Code Playgroud)

  • array_map()返回一个数组,而$ row不是通过引用传递的,所以这行本质上什么都不做,不是吗? (3认同)
  • 创建表格的智能方式。我根据您的代码创建了一个有用的函数。谢谢 (2认同)
  • @Timo 视图/控制器类型框架非常适合中型到大型项目(Twig 是一种很棒的语言和工具)。对于较小的项目,我认为混合代码是可以的,但永远不要碰“?&gt;”功能,永远不要关闭 php 解释器。你将以令人尴尬的意大利面条式代码结束。在雇用时,这样的代码是我毫无疑问地拒绝一个人的理由。 (2认同)
  • @timo 只是不要关闭 PHP 解释器,这是 1994 年以来极其丑陋的脚本。只需创建一个输出 HTML (echo) 的函数或将 echo 放入您的代码中即可。如果您的项目变得更大,请使用树枝之类的东西。 (2认同)

Gho*_*ell 30

这是我的:

<?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'=>'tom@example.org', 'company'=>'example ltd'),
    array('first'=>'hugh', 'last'=>'blogs', 'email'=>'hugh@example.org', 'company'=>'example ltd'),
    array('first'=>'steph', 'last'=>'brown', 'email'=>'steph@example.org', 'company'=>'example ltd')
);

echo build_table($array);
?>
Run Code Online (Sandbox Code Playgroud)


bas*_*eck 14

   <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>
Run Code Online (Sandbox Code Playgroud)

  • 模板标签有什么问题?@HalfCrazed (5认同)
  • 短标签并非在所有系统上都可用 (3认同)

小智 7

您也可以使用array_reduce

array_reduce —使用回调函数将数组迭代地减少为单个值

例:

$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>";
Run Code Online (Sandbox Code Playgroud)


Int*_*arX 6

这是最好、最简单和最有效的方法之一。您可以将数组转换为具有任意数量的列或行的表。它将数组键作为表头。不需要array_map。

function array_to_table($matriz) 
{   
   echo "<table>";

   // Table header
        foreach ($matriz[0] as $clave=>$fila) {
            echo "<th>".$clave."</th>";
        }

    // Table body
       foreach ($matriz as $fila) {
           echo "<tr>";
           foreach ($fila as $elemento) {
                 echo "<td>".$elemento."</td>";
           } 
          echo "</tr>";
       } 
   echo "</table>";}
Run Code Online (Sandbox Code Playgroud)


Mik*_*tar 5

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>";
Run Code Online (Sandbox Code Playgroud)


Tob*_*ias 2

构建两个 foreach 循环并迭代数组。打印出该值并在其周围添加 HTML 表格标签。