Blogger Information
Blog 24
fans 0
comment 0
visits 15411
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
4月17号for()while()foreach()实现遍历
小蚂蚁的博客
Original
529 people have browsed it

实例

<?php
echo '<h3>数组的遍历</h3><hr color="green">';

$teacher=['id'=>10086,'name'=>'liangxiansheng','salary'=>300,'course'=>'php'];

// 用for来实现关联数组的遍历
for($i=0; $i<count($teacher);$i++){
	echo '['.key($teacher),']=>',current($teacher),'<br>';
	next($teacher);
}

echo '<hr>';


// while()循环
reset($teacher);
$i=0;
while ($i<count($teacher)) {
	echo '['.key($teacher),']=>',current($teacher),'<br>';
	next($teacher);//循环体
	$i++;//条件更新
}
echo '<hr>';

// foreach($arr os [$key=>]$value){}数组遍历专用
echo '<h3>学生信息</h3>';
echo '<ul>';
foreach ($teacher as $key => $value) {
	echo '<li>'.$key.':'.$value.'</li>';
}
echo '</ul>';

echo '<hr color="green">';
echo '<table border="1" cellpadding="3" cellspacin="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 $key => $value) {
	echo '<td align="center">'.$value.'</td>';
}
echo '</tr>';
echo '</table>';


// 创建一个生日选择器
echo '<p>选择你的生日</p>';
$years=range(1950,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="year">';
foreach ($months as $value) {

echo '<option value="'.$value.'">'.sprintf("%02d",$value).'</option>';
}
echo '</select>';

echo '  ';
$days=range(1,31);//生成指定范围的一个数组   日
echo '<select name="year">';
foreach ($days as $value) {

echo '<option value="'.$value.'">'.sprintf("%02d",$value).'</option>';
}
echo '</select>';

echo '  ';

echo '  ';


 echo '<button>提交</button>';

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post