<?php $arr = [ ['name'=>'php','age'=>19,'sex'=>'男','addr'=>'河北'], ['name'=>'html','age'=>29,'sex'=>'女','addr'=>'北京'], ['name'=>'css','age'=>19,'sex'=>'男','addr'=>'上海'], ['name'=>'java','age'=>19,'sex'=>'女','addr'=>'天津'], ['name'=>'js','age'=>19,'sex'=>'男','addr'=>'秦皇岛'], ]; //获取总数 $total = count($arr); $title = "户口调查"; function createTable($arr){ $str = ''; foreach ($arr as $key => $value) { $str .= '<tr>'; $str .= '<td>'.$value['name'].'</td>'; $str .= '<td>'.$value['age'].'</td>'; $str .= '<td>'.$value['sex'].'</td>'; $str .= '<td>'.$value['addr'].'</td>'; $str .= '</tr>'; } return $str; } ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title><?php echo $title; ?></title> <style> *{margin: 0;padding: 0} table,tr,th,td{ border: 1px solid #ccc; padding:8px; } table{margin:20px;text-align: center;} table tr th{ background: lightblue; } table caption{ background: lightgreen; color:#fff; } </style> </head> <body> <table> <caption> <?php echo $title?> </caption> <tr> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>地址</th> </tr> <?php echo createTable($arr); ?> </table> </body> </html>
代码显示如下: