当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > each() 函数
The each() function returns the current element key and value, and moves the internal pointer forward.
each()函数的作用是:返回数组中当前的键和值并将数组指针向前移动一步
This element key an value is returned in an array with four elements. Two elements (1 and Value) for the element value, and two elements (0 and Key) for the element key.
使用该函数将从数组中返回的四个元素的键和值,其中两个元素的键分别为“1”和“value”,而另外两个元素的键分别为“0”和“key”。(见案例1)
This function returns FALSE if there are no more array elements.
如果不包含数组元素,则该函数返回FALSE。
each(array) |
Parameter参数 | Description描述 |
---|---|
array | Required. Specifies the array to use 必要参数。指定需要执行操作的数组对象 |
Note: This function returns FALSE on empty elements or elements with no value.
注意:如果元素不存在或元素值为空,那么这个函数返回FALSE。
<?php $people = array("Peter", "Joe", "Glenn", "Cleveland"); print_r (each($people)); ?> |
The output of the code above will be:
上述代码将输出下面的结果:
Array ( [1] => Peter [value] => Peter [0] => 0 [key] => 0 ) |
Same example as above, but with a loop to output the whole array:
例子与案例1相同,使用循环语句输出整个数组:
<?php $people = array("Peter", "Joe", "Glenn", "Cleveland"); reset($people); while (list($key, $val) = each($people)) { echo "$key => $val<br />"; } ?> |
The output of the code above will be:
上述代码将输出下面的结果:
0 => Peter 1 => Joe 2 => Glenn 3 => Cleveland |