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