Home > php教程 > php手册 > body text

php数组函数序列之each()

WBOY
Release: 2016-06-13 12:04:35
Original
830 people have browsed it

each()定义和用法
each() 函数生成一个由数组当前内部指针所指向的元素的键名和键值组成的数组,并把内部指针向前移动。

返回的数组中包括的四个元素:键名为 0,1,key 和 value。单元 0 和 key 包含有数组单元的键名,1 和 value 包含有数据。

如果内部指针越过了数组范围,本函数将返回 FALSE。

语法
each(array)参数 描述
array 必需。规定要使用的数组。

例子 1

复制代码 代码如下:


$people = array("Peter", "Joe", "Glenn", "Cleveland");
print_r (each($people));
?>


输出:

Array ( [1] => Peter [value] => Peter [0] => 0 [key] => 0 )例子 2
each() 经常和 list() 结合使用来遍历数组。本例与上例类似,不过循环输出了整个数组:

复制代码 代码如下:


$people = array("Peter", "Joe", "Glenn", "Cleveland");
reset($people);
while (list($key, $val) = each($people))
{
echo "$key => $val
";
}
?>


输出:

0 => Peter
1 => Joe
2 => Glenn
3 => Cleveland例子解释
因为将一个数组赋值给另一个数组时会重置原来的数组指针,因此在上例中如果我们在循环内部将 $people 赋给了另一个变量的话将会导致无限循环。
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!