php 迭代器的学习

在PHP中有一些预定义的类,比如迭代器类,有SPL提供。常用的几个类:

  1. Iterator------最基本的迭代器
  2. IteratorAggregate --------可以提供一个迭代器的对象,但它本身并不是一个迭代器
  3. RecursiveIterator----------用来遍历RecursiveIterators
  4. FilterIterator-------可以对数据进行过滤的迭代器,值返回与过滤器想匹配的数据
  5. RegexIterator-------FilterIterator中一个内置的具体实现,他是用正则表达式作为过滤器
  6. MultipeIterator-------可以依次遍历多个迭代的迭代器
  7. LimitIterator--------对其数据子集的迭代进行限制的过滤器(类似于SQL中的LIMIT、OFFSET 和 COUNT)

Iterator 举例:

class BasicIterator implements Iterator{ 
    private $key = 0;
    private $data = array(
        'hello','world'
    );

    public function __construct(){
        $this->key = 0;
    }

    public function rewind(){    //用于将指针重新移到起始位置
        $this->key = 0;        
    }

    public function current(){   //获取当前位置的值,并赋给foreach 中 $value  位置
        return $this->data[$this->key];
    }

    public function key(){      //获取当前的key ,并赋给foreach 中 $key  位置
        return $this->key;
    }

    public function next(){    //将指针移到下一个位置
        $this->key++;
        return true;
    }

    public function valid(){    //foreach循环结束的根据
        return isset($this->data[$this->key]);
    }
}

$iterator = new BasicIterator();

foreach ($iterator as $key => $value) {
    echo "  ".$key."   ".$value."\n";
} 
//输出:
0  hello
1  world

RecursiveIterator 举例:

$arr = [
    'hello',
    ['world'],
    ['how',
        ['are','you',['THIS','IS THIRD']]
    ],
    'doing'
];    //定义一个多级的数组

$recursive = new RecursiveArrayIterator($arr);  //该迭代器允许在与ArrayIterator相同的方式迭代数组和对象时取消设置和修改值和键。(引用php manual的解释)
$recursiveIterator = new RecursiveIteratorIterator($recursive); //可以用来遍历递归迭代器

foreach($recursiveIterator as $key=>$vlaue){
    echo "  Depath:".$recursiveIterator->getDepth();
    echo "  Key:".$key;
    echo "  Value:".$vlaue.PHP_EOL;
}
输出:
Depath:0    Key:0   Value:hello
Depath:1    Key:0   Value:world
Depath:1    Key:0   Value:how
Depath:2    Key:0   Value:are
Depath:2    Key:1   Value:you
Depath:3    Key:0   Value:THIS
Depath:3    Key:1   Value:IS THIRD
Depath:0    Key:3    Value:doing
FilterIterator  实例:
class EvenFilterIterator extends FilterIterator{

    public function accept(){    //定义过滤规则:过滤key为奇数的元素
        $iterator = $this->getInnerIterator();
        $key = $iterator->key();
        return $key%2==0;
    }
}

$arr = array('hello','Everybody','I\'m','Amazing','The','Who','Doctor','Lives');

$arrIterator = new ArrayIterator($arr);    //现将定义的数组变成数组迭代对象,
$filter = new EvenFilterIterator($arrIterator);  //这个类只接受迭代对象作为参数,实例化过滤类
 foreach ($filter as $key => $value) { echo " Key:".$key." "; echo " Value:".$value.PHP_EOL; }
输出:
Key:0   Value:hello
Key:2   Value:I'm
Key:4   Value:The
Key:6   Value:Doctor

多个迭代器组合使用:

  

$arr = [
    'hello',
    ['world'],
    ['how',
        ['are','you',['THIS','IS THIRD']]
    ],
    'doing'
];

$recursive = new RecursiveArrayIterator($arr);
$recursiveIterator = new RecursiveIteratorIterator($recursive);
$limitIterator = new LimitIterator($recursiveIterator,2,5);

foreach ($limitIterator as $key => $value) {
    $innerIterator = $limitIterator->getInnerIterator();
    echo " Depath:".$innerIterator->getDepth()."    ";
    echo " Key:".$key."    ";
    echo " Value:".$value.PHP_EOL;
}
输出:
Depath:1   Key:0   Value:how
Depath:2   Key:0   Value:are
Depath:2   Key:1   Value:you
Depath:3   Key:0   Value:THIS
Depath:3   Key:1   Value:IS THIRD

  

posted @ 2017-07-31 00:23  newAdmin  阅读(273)  评论(0编辑  收藏  举报