Blogger Information
Blog 87
fans 0
comment 0
visits 59714
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
4月17日作业:For(),while(),foreach()实现索引数组,关联数组的遍历
黄忠倚的博客
Original
602 people have browsed it

实例

<?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>';

运行实例 »

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


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