Blogger Information
Blog 30
fans 2
comment 3
visits 20140
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
索引数组/关联数组的遍历——2018年4月17日
jackallen的博客
Original
657 people have browsed it

一、代码案例及演示:

实例

<?php
header("Content-type:text/html;charset=utf-8");

for ($i=0; $i<10; $i++) {
    echo $i<9 ? $i. ',' : $i;
}
echo "<hr>";

$i = 0;
while ($i <= 10) {
    echo $i<9 ? $i.',' : $i;
    $i++;
}
echo '<hr>';
echo '<pre>';
$user = ['name'=>'hah', 'id'=>5, 'sex'=>'男'];
print_r($user);
echo '<hr color="red">';
echo in_array('hah',$user) ? '存在<br>' : '不存在';
echo '<hr>';

$teacher = ['id'=>1, 'name'=>'王强', 'salary'=> 3000, 'course'=>'java'];

//用for循环来实现关联数组的遍历
for ($i=0; $i<count($teacher); $i++) {
    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 '<h4>工资列表</h4>';
echo '<ul>';
foreach ($teacher as $key => $value) {
    echo '<li>'.$key.':'.$value.'</li>';
}
echo '</ul>';
echo '<hr color="red">';
//如果只对值感兴趣
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>';

运行实例 »

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


Correction status:qualified

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