Correction status:Uncorrected
Teacher's comments:
For(), while(),foreach(),实现索引数组,关联数组的遍历:
<?php echo '<h2>学习for(),while(),foreach()三种循环</h2>'; //$a = 10; //for($i=0;$i<$a;$i++){ //循环语句 //} // $a=10; // for ($i=0; $i <$a ; $i++) { // echo $i<9 ? $i.',' : $i; // } $student_name = [1 => '小明' , 2 => '小红' , 3 => '小刚' , 4 => '小鹏' , 5 => '小秦']; // for ($i=0; $i < count($student_name); $i++) { // echo '<pre>'; // print '['.key($student_name).'=>'.current($student_name).']'; // next($student_name); // } $table = '<table border="1" cellpadding="3" cellspacing="0" width="60%" >'; $table .= '<caption style="color:red"><strong>同学录<strong></caption>'; $table .= '<tr>'; for ($i=0; $i < count($student_name); $i++) { $table .= '<th bgcolor="pink">'.key($student_name).'</th>'; next($student_name); } $table .= '</tr>'; reset($student_name); $table .= '<tr>'; for ($a=0; $a < count($student_name); $a++) { $table .= '<td align="center">'.current($student_name).'</td>'; next($student_name); } $table .= '</tr>'; $table .='</table>'; echo $table; // while (条件语句) { // 输出的内容 // } echo '<hr color="blue">'; // $form = '<form action="" method="">'; // $form .='<p><label for="key">同学ID:</label><input type="text" name="key" id="key" value=""></p>'; // $form .='<p><label for="current">同学小名:</label><input type="text" name="current" id="current"></p>'; // $form .='<button type="submit">提交</button>'; // $form .='</form>'; // echo $form; echo '<hr color="green">'; $game_name=['吃鸡'=>'绝地求生' , 'LOL' => '英雄联盟', '农药'=>'王者荣耀' ,'DNF'=>'地下城勇士']; // echo '<pre>'; // var_dump($game_name); $table = '<table border="1" cellpadding="3" cellspacing="0" width="60%" >'; $table .= '<caption style="color:red"><strong>开心时刻<strong></caption>'; $table .= '<tr>'; $i=0; while ($i < count($game_name)) { $table .= '<th bgcolor="lightgreen">'.key($game_name).'</th>'; next($game_name); $i++; } $table .= '</tr>'; reset($game_name); $table .= '<tr>'; $b=0; while ($b<count($game_name)) { $table .= '<td align="center">'.current($game_name).'</td>'; next($game_name); $b++; } $table .= '</tr>'; $table .='</table>'; echo $table; echo '<hr color="yellow">'; // foreach ($variable as $key => $value) { # code... // } $name=['齐天大圣'=>'孙悟空' , '天蓬元帅' => '猪八戒', '卷帘大将'=>'沙僧' ,'师傅'=>'唐僧']; foreach ($name as $key => $value) { echo '<pre>'; echo '['.$key.'=>'.$value.']'; } echo '<ul>'; foreach ($name as $key => $value) { echo '<li>'.$key.":".$value.'</li>'; } echo '</ul>'; foreach ($name as $value) { echo '<li>'.$value.'</li>'; } echo '</ul>'; foreach ($name as $key) { echo '<li>'.$key.'</li>'; } echo '</ul>'; echo '<hr color="blue">'; echo '<p><h2>个人信息表</h2></p>'; echo '<p><label for="name">姓名:</label><input type="text" name="name" id="name" value="覃正红"></p>'; echo '<p><label for="male">性别:</label><input type="radio" name="male" id="male" value="" checked><label for="male">男</label></p>'; echo '<p>爱好:<input type="checkbox" name="male" id="hobby" value="" checked><label for="hobby">篮球</label><input type="checkbox" name="male" id="football" value=""><label for="football">足球</label> </p>'; echo '<span >生日信息:</span>'; $years = range(1980, 2018); echo '<select>'; foreach ($years as $value) { echo '<option value="$value">'.$value.'</option>'; } echo '</select name="years">'; echo ' '; $months = range(1, 12); echo '<select>'; foreach ($months as $value) { echo '<option value="'.$value.'">'.$value.'</option>'; } echo '</select>'; echo ' '; $days = range(1, 31); echo '<select>'; foreach ($days as $value) { echo '<option value="'.$value.'">'.$value.'</option>'; } echo '</select>'; echo '<br>'; echo '<span>生肖:</span>'; $propertys = ['鼠','牛','虎','兔','龙','蛇','马','羊','猴','鸡','狗','猪']; echo '<select>'; foreach ($propertys as $value) { echo '<option value="'.$value.'">'.$value.'</option>'; } echo '</select>';
点击 "运行实例" 按钮查看在线实例
实行效果图:
array_splice()的函数的用法: