Correction status:Uncorrected
Teacher's comments:
<?php echo '<h3>for来实现对关联数组的遍历</h3><hr color="green">'; $arr = ['id'=>1, 'name'=>'yl', 'age'=> '20', 'sex'=>'man']; //用for循环来实现关联数组的遍历 for ($i=0; $i<count($arr); $i++) { echo key($arr),'=>',current($arr),'<br>'; next($arr); } echo '<h3>用while循环来实现对关联数组的遍历</h3><hr color="green">'; reset($arr); $i = 0; while ($i<count($arr)) { echo key($arr),'=>',current($arr),'<br>'; next($arr); $i++; } echo '<h3>用foreach实现对关联数组的遍历</h3><hr color="green">'; echo '<h4>个人信息</h4>'; echo '<ul>'; foreach ($arr as $key => $value) { echo '<li>'.$key.':'.$value.'</li>'; } echo '</ul>'; //如果只对值感兴趣 echo '<table border="1" cellpadding="3" cellspacing="0" width="300">'; echo '<caption>个人信息表</caption>'; echo '<tr bgcolor="lightskyblue"><th>ID</th><th>姓名</th><th>年龄</th><th>姓名</th></tr>'; echo '<tr>'; foreach ($arr as $value) { echo '<td align="center">'.$value.'</td>'; } echo '</tr>'; echo '</table>';
点击 "运行实例" 按钮查看在线实例