实例
<?php echo '<h2>数组的遍历</h2><hr color="green>'; $teacher = ['id'=>1001,'name'=>'Kevin','salary'=>3000,'course'=>'php']; //用for来实现对关联数组的遍历 for ($i=0; $i<count($teacher); $i++) { //这个count写错了,全局不显示,所以不要写错哦! echo '['.key($teacher),'] =>',current($teacher),'<br>'; next($teacher); } echo '<hr color="red">'; //while() reset($teacher); $i = 0; while ($i<count($teacher)) { echo '['.key($teacher),'] =>',current($teacher),'<br>'; next($teacher); $i++; } echo '<hr color="red">'; //foreach($arr as [$key =>] $value) { echo '<h3>讲师信息</h3>'; echo '<ul>'; foreach ($teacher as $key => $value) { echo '<li>'.$key.':'.$value.'</li>'; } echo '<ul>'; echo '<hr color="red">'; echo '<table border="1" cellpadding="0" width="300">'; echo '<caption>讲师信息表</caption>'; echo '<tr bgcolor="lightskyblue"><th>ID</th><th>姓名</th><th>工资</th><th>课程</th></tr>'; echo '<tr>'; foreach ($teacher as $value) { echo '<td align="center">'.$value.'</td>'; } echo '</tr>'; echo '</table>'; echo '<hr color="red">'; // 创建一个生日选择器 echo '<p>选择你的生日</p>'; $years = range(1980,2000); echo '<select name="year">'; foreach ($years as $value) { echo '<option value="'.$value.'" >'.$value.'</option>'; } echo '</select>'; echo ' '; $months = range(1,12); echo '<select name="months">'; foreach ($months as $value) { echo '<option value="'.$value.'" >'.sprintf("%02d",$value).'</option>'; } echo '</select>';
运行实例 »
点击 "运行实例" 按钮查看在线实例