foreach遍历表格输出的两种方法

直接贴代码如下:

<?php
$data = [
    ['id'=>001,'name'=>'kang','age'=>25,'salary'=>9000],
    ['id'=>002,'name'=>'xin','age'=>24,'salary'=>8000],
    ['id'=>003,'name'=>'lin','age'=>3,'salary'=>7000],
    ['id'=>004,'name'=>'yi','age'=>2,'salary'=>6000],
];
?>

<document html>
    <html lang="en">
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
    <h3 align="center">用户信息表</h3>
    <table border="1" cellpadding="0" cellspacing="0" width="50%" align="center">
        <tr>
            <th>id</th>
            <th>name</th>
            <th>age</th>
            <th>salary</th>
        </tr>
        <?php 

        foreach ($data as $value) {
            echo '<tr>';
            echo '<th>'.$value['id'].'</th>';
            echo '<th>'.$value['name'].'</th>';
            echo '<th>'.$value['age'].'</th>';
            echo '<th>'.$value['salary'].'</th>';
            echo '</tr>';

        } 

        ?>
        
        <?php foreach ($data as $value): ?>
        <tr>
            <th><?php echo $value['id'];?></th>
            <th><?php echo $value['name'];?></th>
            <th><?php echo $value['age'];?></th>
            <th><?php echo $value['salary'];?></th>
        </tr>
    <?php endforeach?>
    </table>
</body>

 

posted @ 2021-01-16 10:38  secsafe  阅读(1144)  评论(0编辑  收藏  举报