实例1.数组的应用
<?php $title = '员工信息表'; //声明变量表头 $TableTitle = ['编号', '姓名', '年龄', '性别', '邮箱']; //创建索引数组 $i = count($TableTitle); //count() 函数返回数组中元素的数目。 $table = [ ['id' => 1, 'name' => '刘备', 'age' => 25, 'sex' => '1', 'mail' => 'liuhuangshu@qq.com'], ['id' => 2, 'name' => '张飞', 'age' => 20, 'sex' => '1', 'mail' => 'dazuizhang@qq.com'], ['id' => 3, 'name' => '赵云', 'age' => 18, 'sex' => '1', 'mail' => 'zilong@qq.com'], ['id' => 4, 'name' => '关羽', 'age' => 35, 'sex' => '1', 'mail' => 'erye@qq.com'], ['id' => 5, 'name' => '孙尚香', 'age' => 16, 'sex' => '0', 'mail' => 'xiangxiang@qq.com'], ]; //创建二维的关联数组 ?> <table width="600" border="1"> <caption><h1><?= $title; ?></h1></caption> <thead> <tr> <?php for ($x = 0; $x < $i; $x++) { //创建for循环,x 变量为0 ,如果x变量的值小于索引数组的数目,则x加1,并循环一次 ?> <th><?= $TableTitle[$x]; ?></th> <!--因为变量 x 的默认值小于索引数组的数目,则加了1 ,然后这里读取索引数组的对应数据--> <?php } ?> <!-- 结束 for 循环--> </tr> </thead> <?php foreach ($table as $aa): ?> <tr> <td><?php echo $aa['id']; ?></td> <td><?php echo $aa['name']; ?></td> <td><?php echo $aa['age']; ?></td> <td><?php echo $aa['sex']; ?></td> <td><?php echo $aa['mail']; ?></td> </tr> <?php endforeach; ?> </table>
运行实例 »
点击 "运行实例" 按钮查看在线实例