Blogger Information
Blog 64
fans 2
comment 3
visits 75568
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
For(), while(),foreach(),实现索引数组,关联数组的遍历—2018年4月18日09:26:37
清雨的博客
Original
969 people have browsed it

循环在网站开发中是经常用到的,最为典型的就是使用循环将文字等展示到前台。

实例

<?php

$user = ['id'=>1,'name'=>'清雨','age'=>29,'trade'=>'互联网','specialty' =>'PHP','area'=>'内蒙古'];
echo '<hr color="red">';
echo '<h2>采用foreach()循环来实现关联数组的遍历</h2>';
foreach ($user as $key => $value) {
    echo $key.'=>'.$value. '<br>';
}
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 ($user as $value) {
    echo '<td align="center">'.$value.'</td>';
}
echo '</tr>';
echo '</table>';
echo '<hr color="red">';
echo "<h2>采用while()循环来实现关联数组的遍历</h2>";
  reset($user);
  $i = 0;
  while ($i<count($user)) {
	echo key($user),'=>',current($user),'<br>';
	next($user);
	$i++;
 }
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><th>专业</th><th>地区</th></tr>';
echo '<tr>';
  reset($user);
  $i = 0;
  while ($i<count($user)) {
	echo '<td align="center">'.current($user).'</td>';
	next($user);
	$i++;
 }
echo '</tr>';
echo '</table>';
echo '<hr color="red">';
echo "<h2>采用For()循环来实现关联数组的遍历</h2>";
reset($user);
for ($i=0; $i < count($user); $i++) {
	echo key($user),'=>',current($user),'<br>';
	next($user);
}
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>';
reset($user);
for ($i=0; $i <count($user); $i++) { 
	echo '<td align="center">'.current($user).'</td>';
	next($user);
}
echo '</tr>';
echo '</table>';

运行实例 »

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

效果图

QQ截图20180418092742.png

手抄代码:

2.jpg

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