批改状态:未批改
老师批语:
一、学习知识点
1,使用for循环将数组内的全部内容遍历。
2,是while循环将数组的内容全部遍历。
3,重点:使用foreach方法遍历数组,后期数据库查询较多的使用。
二、效果截图

三、源码
<?php
//一·数组遍历方法学习
//使用 for ,while, foreach 三种方式分别实现数组内容遍历
//1,使用for循环
//要求实现将学生信息遍历输出展示
$student_array=['no'=>1001,'name'=>'周亮','ege'=>21,'class'=>'六年级'];
for ($i=0; $i < count($student_array); $i++) { //for循环语句
echo '['.key($student_array).'=>'.current($student_array).']'.'<br>';//获取键值和键名
next($student_array);//移动指针
}
echo "<hr>";
//2,使用while循环
//要求实现将学生信息遍历输出展示
reset($student_array);
$a=0;
while ($a<count($student_array)) {
'['.key($student_array).'=>'.current($student_array).']'.'<br>';//获取键值和键名
next($student_array);//移动指针
$a++;
}
echo "<hr>";
//3,使用foreach遍历数组
//要求实现将用户遍历输出展示
$student_array=['id'=>1000,'username'=>'zf','password'=>'z123456','name'=>'小凡'];
echo '<table cellspacing="0" cellpadding="3" border="1px solid red">';
echo '<tr align="center" bgcolor="#FFBBFF">';
echo "<td>id</td><td>用户名</td><td>密码</td><td>姓名</td>";
echo "</tr>";
echo "<tr>";
foreach ($student_array as $value) {
echo "<td>{$value}</td>";
}
echo "</tr>";
echo "</table>";
//二、array_splice() 函数从数组中移除选定的元素,并用新元素取代它。该函数也将返回包含被移除元素的数组。
//array_splice(array,start,length,array)
//将数组第一个元素替换掉
print_r(array_splice($student_array,0,1,array('id'=>3333)));//将第一个元素id替换成3333
echo "<hr>";
print_r($student_array);//打印一下新数组
?>点击 "运行实例" 按钮查看在线实例
四、手抄代码

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号