php8.5
Home/ Manual/ spl / norewinditerator/ NoRewindIterator::__construct

NoRewindIterator::__construct

PHP function Edit on GitHub ✎

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

Construct a NoRewindIterator

Description

NoRewindIterator::__construct(Iterator $iterator)

Constructs a NoRewindIterator.

Parameters

iterator

The iterator being used.

Examples

NoRewindIterator::__construct example

The second loop does not output because the iterator is only used once, as it does not rewind.

php
<?php
$fruit = array('apple', 'banana', 'cranberry');

$arr = new ArrayObject($fruit);
$it  = new NoRewindIterator($arr->getIterator());

echo "Fruit A:\n";
foreach( $it as $item ) {
    echo $item . "\n";
}

echo "Fruit B:\n";
foreach( $it as $item ) {
    echo $item . "\n";
}
?>

The above example will output something similar to:

output
Fruit A:
apple
banana
cranberry
Fruit B:

See Also

  • NoRewindIterator::valid

Source: reference/spl/norewinditerator/construct.xml · from the official PHP manual (php/doc-en)