php数组函数序列之each()
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 赋给了另一个变量的话将会导致无限循环。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor
