这是我自己写的简单的迭代器的使用,对对向的遍历
<?php
/***对象的遍历,
*需要实现系统预定的接口iterator
*iterator(迭代器)
**/
class Arr implements iterator{
private $pri=array('name'=>'zhangsan','age'=>'20','sex'=>'male');
function current(){
return current($this->pri);
}
function key(){
return key($this->pri);
}
function rewind(){
reset($this->pri);
}
function next(){
return next($this->pri);
}
function valid(){
return !is_null(key($this->pri));
}
}
$obj =new Arr();
echo "<pre>";
foreach($obj as $k=>$v){
echo $k."=>".$v,"<br/>";
}
示例: