当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > each() 函数

PHP
WINDOWS下安装MySQL
PHP 制作 网站/服务器 监视脚本
用PHP和CSS制作活动按钮
PHP 单件模式
PHP MVC模式,类封装以及HACK
PHP 中使用正则表达式
PHP 防止 SQL 注入攻击
PHP 跨站点脚本攻击
PHP 防止用户操纵 GET 变量
PHP 防止远程表单提交

PHP 中的 each() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 453 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

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。

Syntax
语法

each(array)

Parameter参数 Description描述
array Required. Specifies the array to use
必要参数。指定需要执行操作的数组对象


Tips and Notes
注意点:

Note: This function returns FALSE on empty elements or elements with no value.
注意:如果元素不存在或元素值为空,那么这个函数返回FALSE。


Example 1
案例1

<?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 )


Example 2
案例2

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

评论 (0) All

登陆 | 还没注册?