Blogger Information
Blog 7
fans 0
comment 1
visits 5755
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组遍历
bokewinner
Original
799 people have browsed it

foreach函数遍历

$arr = array('Tom'=>12,'Mary'=>14,'John'=>15);
foreach($arr as $key => $value)
{
    echo "{$key}的年龄为:{$value}";
    echo "<br/>";
}

for+有关指针函数遍历

$arr = array('Tom'=>12,'Mary'=>14,'John'=>15);
$len = count($arr);
for($i = 0;$i < $len;$i++)
{
    $key = key($arr);//获取当前单元的键
    $value = current($arr);//获取当前单元的值
    echo "{$key}的年龄为:{$value}";
    next($arr);//指针指向下一个单元
}

while+list()+each()遍历

while(list($key,$value) = each($arr))
//把each($arr)存储在数组中的下标为0,1的值赋给list()中的变量,并按顺序赋给$key,$value
{
    echo "{$key}的年龄为:{$value}";
}
//list($变量1,$变量2,$变量3...)=$数组:把数组中下标为0,1,2...的值顺序赋给$变量1,$变量2,$变量3...
//each($数组):获取当前单元的值和键($key和$value)并数组指针向下移动一位(存储在数组方式:数字下标和字符串下标)
eg:$arr = array('Tom'=>12,'Mary'=>14,'John'=>15);
each($arr)
结果:
(
0=>'Tom',
[key]=>'Tom',
1=>12,
[value]=>12
)


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