* 数组遍历
* 1. foreach($arr as $key => $value) {}
* 2. 原理:将数组$arr按键值对方式依次取出到$key=>$value中,逐个处理,类似于回调
* 3. 如果只对值处理,可以省略$key: foreach($arr as $value) {}
* 4. 特殊适合关联数组,当然也适用于索引数组$teacher = ['id'=>1001, 'name'=>'peter zhu', 'salary'=> 3000, 'course'=>'php'];
//用for循环来实现关联数组的遍历for ($i=0; $i
echo key($teacher),'=>',current($teacher),'
';
next($teacher);
}
echo '
';
//用while循环来实现reset($teacher);
$i = 0;
while ($i
echo key($teacher),'=>',current($teacher),'
';
next($teacher);
$i++;
}
echo '
';
//foreach($arr as $key=>$value):数组专用的遍历语法结构
echo '
讲师信息
';echo '
- ';
foreach ($teacher as $key => $value) {
echo '
'.$key.':'.$value.'';}
echo '
';echo '
';
//如果只对值感兴趣
echo '
echo '
讲师信息表';echo '
ID姓名工资课程';echo '
';foreach ($teacher as $value) {
echo '
'.$value.'';}
echo '
';echo '
';echo '
';
//创建一个日期选择器
echo '
选择你的生日:
';//生成年$years = range(1980, 2000);
echo '';
foreach ($years as $value) {
echo ''.$value.'年';
}
echo '';
echo ' ';
//生成月$months = range(1, 12);
echo '';
foreach ($months as $value) {
echo ''.sprintf("%02d",$value) .'月';
}
echo '';
echo ' ';
//生成日$days = range(1, 31);
echo '';
foreach ($days as $value) {
echo ''.sprintf("%02d",$value) .'日';
}
echo '';
echo ' ';
echo '提交';
本文原创发布php中文网,转载请注明出处,感谢您的尊重!