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

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 中的 each() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 452 ::
收藏到网摘: 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

登陆 | 还没注册?