将数组中的数据读入html表

时间:2016-02-17 02:31:08

标签: php html arrays html-table

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [code] => ID 6401
                    [name] => Joseph

                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [code] => ID 6597
                    [name] => Peter
                )

        )

我想要打印HTML表格,如:

代码|姓名

ID 6401 |约瑟夫
ID 6597 |彼得

总计:2人

2 个答案:

答案 0 :(得分:2)

使用这个:

<table>
    <thead>
        <tr>
            <th>Code</th><th>Name</th>
        </tr>
    </thead>
    <tbody>
<?php
    foreach($users as $u){
        echo "<tr><td>". $u[0]['code'] ."</td><td>". $u[0]['name'] ."</td></tr>";
    };
    echo "<tr><td>Total :</td><td>".count($users)."</td></tr>";
?>
    </tbody>
</table>

答案 1 :(得分:0)

尝试使用foreach并指定operator + =将是帮助。

<?php
$user = array(
array(
    array(
    "code" => "ID 6401",
    "name" => "Joseph"
    )
),
array(
      array(
    "code" => "ID 6597",
    "name" => "Peter"
    )
));

 ?>
<table>
<thead><th>Code</th><th>Name</th></thead>
<tbody>
<?php 
$count = 0;
foreach ($user as $v){
  echo "<tr><td>".$v[0]['code']."</td><td>".$v[0]['name']."</td></tr>";
  $count += 1;  
}
  echo "<tr><td>Total:</td><td>".$count."</td></tr>";
?>
</tbody>