Blogger Information
Blog 26
fans 0
comment 0
visits 19433
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
4.17For(), while(),foreach(),实现索引数组,关联数组的遍历
十一
Original
661 people have browsed it

1111.png

实例

<?php 
echo '<h3>for来实现对关联数组的遍历</h3><hr color="green">';
$arr = ['id'=>1, 'name'=>'yl', 'age'=> '20', 'sex'=>'man'];

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

echo '<h3>用while循环来实现对关联数组的遍历</h3><hr color="green">';
reset($arr);
$i = 0;
while ($i<count($arr)) {
	echo key($arr),'=>',current($arr),'<br>';
	next($arr);
	$i++;
}

echo '<h3>用foreach实现对关联数组的遍历</h3><hr color="green">';
echo '<h4>个人信息</h4>';
echo '<ul>';
foreach ($arr as $key => $value) {
    echo '<li>'.$key.':'.$value.'</li>';
}
echo '</ul>';
//如果只对值感兴趣
echo '<table border="1" cellpadding="3" cellspacing="0" width="300">';
echo '<caption>个人信息表</caption>';
echo '<tr bgcolor="lightskyblue"><th>ID</th><th>姓名</th><th>年龄</th><th>姓名</th></tr>';
echo '<tr>';
foreach ($arr as $value) {
    echo '<td align="center">'.$value.'</td>';
}
echo '</tr>';
echo '</table>';

运行实例 »

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


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