html5数组中的表

时间:2015-06-27 15:52:15

标签: php arrays html5 html-table

我需要这个阵列最薄的解决方案

$users = array(
    array('id' => '1', 'first_name' => 'Amy', 'last_name' => 'Pond', 'gender' => 'Female'),
    array('id' => '2', 'first_name' => 'Rory', 'last_name' => 'Williams', 'gender' => 'Male'),
    array('id' => '3', 'first_name' => 'Luke', 'last_name' => 'Skywalker', 'gender' => 'Male'),
    array('id' => '4', 'first_name' => 'Leia', 'last_name' => 'Organa', 'gender' => 'Female') 
);

3 个答案:

答案 0 :(得分:0)

foreach ($users as $v) {
    echo '<tr>';
    echo '<td>' . $v['id'] . '</td>';
    echo '<td>' . $v['first_name'] . '</td>';   
    echo '<td>' . $v['last_name'] . '</td>';    
    echo '<td>' . $v['gender'] . '</td>';
    echo '</tr>';
}

答案 1 :(得分:0)

用于循环,代码如下所示


    <table border="3px">
        <tr>
            <th>id</th>
            <th>first name</th>
            <th>last name</th>
            <th>gender</th>
        </tr>
    <?php
    $users = array( array('id' => '1', 'first_name' => 'Amy', 'last_name' => 'Pond', 'gender' => 'Female'), array('id' => '2', 'first_name' => 'Rory', 'last_name' => 'Williams', 'gender' => 'Male'), array('id' => '3', 'first_name' => 'Luke', 'last_name' => 'Skywalker', 'gender' => 'Male'), array('id' => '4', 'first_name' => 'Leia', 'last_name' => 'Organa', 'gender' => 'Female') );

    for($i=0;$i<count($users);$i++)
    {
        echo "<tr>";
        echo "<td>".$users[$i]['id']."</td>";
        echo "<td>".$users[$i]['first_name']."</td>";
        echo "<td>".$users[$i]['last_name']."</td>";
        echo "<td>".$users[$i]['gender']."</td>";
        echo "</tr>";
       // echo $values;
    }
    ?>
    </table>

enter image description here

答案 2 :(得分:0)

得出这个答案,即使我们必须添加更多行或输入数据,也可以使用...就像电子邮件... age et al

<?php
    $users = array(
    array('id' => '0', 'first_name' => 'Amy', 'last_name' => 'Pond', 'gender' => 'Female'),
    array('id' => '1', 'first_name' => 'Rory', 'last_name' => 'Williams', 'gender' => 'Male'),
    array('id' => '2', 'first_name' => 'Luke', 'last_name' => 'Skywalker', 'gender' => 'Male'),
    array('id' => '3', 'first_name' => 'Leia', 'last_name' => 'Organa', 'gender' => 'Female') 
);
?>
<?php if (count($users) > 0): ?>
<table>
  <thead>
    <tr>
      <th><?php echo implode('</th><th>', array_keys(current($users))); ?></th>
    </tr>
  </thead>
  <tbody>
<?php foreach ($users as $row): array_map('htmlentities', $row); ?>
    <tr>
      <td><?php echo implode('</td><td>', $row); ?></td>
    </tr>
<?php endforeach; ?>
  </tbody>`enter code here`
</table>
<?php endif; ?>