<?php echo "===Current===\n"; $it = new NoRewindIterator(new ArrayIterator(array(0 => 'A', 1 => 'B', 2 => 'C'))); echo $it->key() . '=>' . $it->current() . "\n"; echo "===Next===\n"; $it->next(); echo "===Foreach===\n"; foreach ($it as $key => $val) { echo "{$key}=>{$val}\n"; } ?> ===DONE=== <?php exit(0);
/** * @param \PDOStatement $stmt */ public function __construct(\PDOStatement $stmt) { $this->_stmt = $stmt; parent::__construct(new \IteratorIterator($this->_stmt)); }
function next() { echo __METHOD__ . "\n"; parent::next(); }
<?php $data = array('Mela', 'Pera', 'Kiwi'); $iterator = new NoRewindIterator(new ArrayIterator($data)); echo $iterator->current(), PHP_EOL; $iterator->next(); echo $iterator->current(), PHP_EOL; $iterator->next(); echo $iterator->current(), PHP_EOL; $iterator->rewind(); echo $iterator->current(), PHP_EOL;
<?php // NoRewindIterator -> can only read the collection once $arr = array(array("name" => "John Abraham", "sex" => "M", "age" => 27), array("name" => "Lily Bernard", "sex" => "F", "age" => 37), array("name" => "Ayesha Siddika", "sex" => "F", "age" => 26), array("name" => "Afif", "sex" => "M", "age" => 2)); $persons = new ArrayObject($arr); $LI = new NoRewindIterator($persons->getIterator()); foreach ($LI as $person) { echo $person['name'] . "<br/>"; //$LI->rewind(); } foreach ($LI as $person) { echo $person['name'] . "<br/>"; $LI->rewind(); } /* output: John Abraham Lily Bernard Ayesha Siddika Afif */