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

PHP
PHP Libxml
PHP Math
PHP Misc
PHP MySQL
PHP SimpleXML
PHP String
PHP XML
PHP Zip
PHP Mail
用PHP5的DirectoryIterators递归扫描目录
PHP 阻止SQL注入式攻击
PHP5面向对象 - 基础 - 类和对象
PHP5面向对象 - 基础 - 类的属性( public )
PHP5面向对象 - 基础 - 类的属性( private )
PHP5面向对象 - 基础 - 方法
PHP5面向对象 - 基础 - 对象的比较
php5面向对象 - 基础 - 构造函数
php5面向对象 - 基础 - 析构函数
用PHP控制用户的浏览器 - ob*函数的使用
PHP PDO 学习笔记

PHP 中的 each() 函数


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

登陆 | 还没注册?