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>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM