php5中的迭代器

jackxiang 2010-9-13 16:30 | |

ArrayObject
ArrayIterator
Recursive Array Iterator
PDO & IteratorIterator
FilterIterator
XML & RecursiveIteratorIterator
SimpleXMLIterator
LimitIterator & ArrayIterator
SPLFileObject
下面是 SPL 提供的其他的迭代器:

* RecursiveIterator
* RecursiveIteratorIterator
* OuterIterator
* IteratorIterator
* FilterIterator
* RecursiveFilterIterator
* ParentIterator
* SeekableIterator
* LimitIterator
* GlobIterator
* CachingIterator
* RecursiveCachingIterator
* NoRewindIterator
* AppendIterator
* RecursiveIteratorIterator
* InfiniteIterator
* RegexIterator
* RecursiveRegexIterator
* EmptyIterator
* RecursiveTreeIterator
* ArrayIterator

//得到 DirectoryIterator中所有的方法
foreach(get_class_methods(new DirectoryIterator('./')) as $key=>$method){
echo $key.'->'.$method.'<br />';
}

//ArrayObject
$array = array('koala', 'kangaroo', 'wombat', 'wallaby', 'emu', 'kiwi', 'kookaburra', 'platypus');
$arrayObj = new ArrayObject($array);
for($iterator = $arrayObj->getIterator();$iterator->valid();$iterator->next()){
echo $iterator->key() . ' => ' . $iterator->current() . '<br />';
}
//目录迭代器
$dir = new DirectoryIterator('F:\xing\public\images');
$arr = array();
while($dir->valid()){
if(!$dir->isDir()){//如果不是目录
$arr[] = $dir->getFilename();//把目录中的所有文件放到数组中
}
$dir->next();
}
echo '<pre>';
print_r($arr);

class RecursiveFileFilterIterator extends FilterIterator{
// 满足条件的扩展名
protected $ext = array('jpg','gif');
//提供 $path 并生成对应的目录迭代器
public function __construct($path) {
parent::__construct(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)));
}
//检查文件扩展名是否满足条件
public function accept() {
$item = $this->getInnerIterator();
if ($item->isFile() && in_array(pathinfo($item->getFilename(), PATHINFO_EXTENSION), $this->ext)){
return true;
}
}
}
foreach (new RecursiveFileFilterIterator('F:\xing\public\images') as $item) { // 实例化
$arr[] = $item->getFilename();
}
echo '<pre>';
print_r($arr);

//RecursiveIteratorIterator  迭代器
$array = array(
array('name'=>'butch', 'sex'=>'m', 'breed'=>'boxer'),
array('name'=>'fido', 'sex'=>'m', 'breed'=>'doberman'),
array('name'=>'girly','sex'=>'f', 'breed'=>'poodle')
);
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $key=>$value){
echo $key.' -> '.$value.'<br />';
}

//FilterIterator  迭代器
$animals = array('koala', 'kangaroo', 'wombat', 'wallaby', 'emu', 'NZ'=>'kiwi', 'kookaburra', 'platypus');
class CullingIterator extends FilterIterator{
public function __construct( Iterator $it ){
parent::__construct( $it );
}
function accept(){
return is_numeric($this->key());
}
}
$cull = new CullingIterator(new ArrayIterator($animals));//过滤key不为数字的值
foreach($cull as $key=>$value){
echo $key.' == '.$value.'<br />';
}
//XML & RecursiveIteratorIterator   SimpleXMLIterator 迭代器
$it = simplexml_load_string($string, 'SimpleXMLIterator');
foreach(new RecursiveIteratorIterator($it, 1) as $name => $data){
echo $name.' -- '.$data.'<br />';

//SplFileObject文件对象,读文件
$file = new SplFileObject("./a.php");
while($file->valid()){
echo $file->current().'<br/>';
$file->next();
}
//LimitIterator 迭代器 & ArrayIterator
$offset = 3;
$limit = 2;
$array = array('koala', 'kangaroo', 'wombat', 'wallaby', 'emu', 'kiwi', 'kookaburra', 'platypus');
$it = new LimitIterator(new ArrayIterator($array), $offset, $limit);
foreach($it as $k=>$v){
echo $it->getPosition().'<br />';
}
//PDO & IteratorIterator
try {
$dsn = new PDO("sqlite2:/home/kevin/html/periodic.sdb");
$stmt = $dsn->prepare('SELECT * FROM periodic ORDER BY atomicnumber');
$stmt->execute();
$it = new IteratorIterator($stmt);
foreach($it as $row){
$arrayObj = new ArrayObject($row);
for($iterator = $arrayObj->getIterator();$iterator->valid();$iterator->next()){
echo $iterator->current() . '<br />';
}
echo '<hr />';
}

$dsn = null;
}catch (PDOException $e){
print "Error!: " . $e->getMessage() . "<br />";
}

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/3496/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!

评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]