PHP手册预定义接口部分指出:要实现迭代器模式,需要在可迭代对象中实现如下接口:
[php] view plaincopy
abstractpublicmixedcurrent( void )
abstractpublicscalarkey( void )
abstractpublicvoidnext( void )
abstractpublicvoidrewind( void )
abstractpublicbooleanvalid( void )
有了这个 。实现迭代器模式就很方便了,一个简单的实例如下:
[php] view plaincopy
class TestIterator implements Iterator {
private $point = 0;
private $data = https://www.04ip.com/post/array(
"one","two","three",
);
public function __construct() {
$this-point = 0;
}
function rewind() {
$this-point = 0;
}
function current() {
return $this-data[$this-point];
}
function key() {
return $this-point;
}
function next() {
++$this-point;
}
function valid() {
return isset($this-data[$this-point]);
}
}
$it = new TestIterator;
foreach($it as $key = $value) {
echo $key, $value;
echo "\n";
}
当然,使用了迭代器的对象可以以如下方式进行遍历:
[php] view plaincopy
$it = new TestIterator;
$it-rewind();
while ($it-valid()){
$key = $it-key();
$value = https://www.04ip.com/post/$it-current();
echo "$key=$value";
$it-next();
}
最后附上YII中ListIterator(顾名思义,实现对List的迭代操作的迭代器)的实现:
[php] view plaincopy
?php
/**
* CListIterator class file.
*
* @author Qiang Xue qiang.xue@gmail.com
* @link
* @copyright Copyright ? 2008-2011 Yii Software LLC
* @license
*/
/**
* CListIterator implements an interator for {@link CList}.
*
* It allows CList to return a new iterator for traversing the items in the list.
*
* @author Qiang Xue qiang.xue@gmail.com
* @version $Id$
* @package system.collections
* @since 1.0
*/
class CListIterator implements Iterator
{
/**
* @var array the data to be iterated through
*/
private $_d;
/**
* @var integer index of the current item
*/
private $_i;
/**
* @var integer count of the data items
*/
private $_c;
/**
* Constructor.
* @param array $data the data to be iterated through
*/
public function __construct($data)
{
$this-_d=$data;
$this-_i=0;
$this-_c=count($this-_d);
}
/**
* Rewinds internal array pointer.
* This method is required by the interface Iterator.
*/
public function rewind()
{
$this-_i=0;
}
/**
* Returns the key of the current array item.
* This method is required by the interface Iterator.
* @return integer the key of the current array item
*/
public function key()
{
return $this-_i;
}
/**
* Returns the current array item.
* This method is required by the interface Iterator.
* @return mixed the current array item
*/
public function current()
{
return $this-_d[$this-_i];
}
/**
* Moves the internal pointer to the next array item.
* This method is required by the interface Iterator.
*/
public function next()
{
$this-_i++;
}
/**
* Returns whether there is an item at current position.
* This method is required by the interface Iterator.
* @return boolean
*/
public function valid()
{
return $this-_i$this-_c;
}
}
PHP 数组遍历方法大全(foreach,list,each)在PHP中数组分为两类:
数字索引数组和关联数组 。
其中数字索引数组和C语言中的数组一样,下标是为0,1 , 2…
推荐阅读
- 网络创业如何上头条推广,如何在头条推广自己的产品
- 微信小程序和HTML5有什么区别,小程序和h5制作页面有什么区别
- 汉川直播运营招聘,武汉直播运营
- go语言探测工具 go语言采集
- 腾讯开通小程序流程怎么走,腾讯小店要怎么开通
- JAVA爬虫群,java爬虫入门教程
- 视频号不做直播怎么引流的简单介绍
- python完美函数 python函数all
- 为什么大海变高山视频,为什么大海看起来是向上上升