首页 > 解决方案 > 同一个生成器上的多个循环 - PHP

问题描述

我需要在多个循环上循环同一个生成器 - 我尝试使用倒带,但出现此错误 - “无法倒带已经运行的生成器”。

$gen = myGenerator();
while ($gen->valid()){
    $document = $gen->current();
    $gen->next();
    //...
}

$gen->rewind(); //this is where i get the error

while ($gen->valid()){
    $document = $gen->current();
    $gen->next();
    //...
}

标签: phpyieldphp-generators

解决方案


推荐阅读