The CachingIterator::rewind() function is an inbuilt function in PHP which is used to rewind the iterator.
Syntax:
void CachingIterator::rewind( void )
Parameters: This function does not accept any parameters.
Return Value: This function does not return any value.
Below programs illustrate the CachingIterator::rewind() function in PHP:
Program 1:
<?php // Declare an ArrayIterator $arr = new ArrayIterator(
array (
"a" => 4,
"b" => 2,
"g" => 8,
"d" => 6,
"e" => 1,
"f" => 9
)
); // Create a new CachingIterator $cachIt = new CachingIterator(
new ArrayIterator( $arr ),
CachingIterator::FULL_CACHE
); // Move to last position $cachIt ->seek(5);
// Display the next value var_dump( $cachIt ->next());
// Move to start position $cachIt -> rewind ();
// Display the current element echo $cachIt ->current();
?> |
Output:
NULL 4
Program 2:
<?php // Declare an ArrayIterator $arr = new ArrayIterator(
array (
"b" => "for" ,
"a" => "Geeks" ,
"e" => "Science" ,
"c" => "Geeks" ,
"f" => "Portal" ,
"d" => "Computer"
)
); // Create a new CachingIterator $cachIt = new CachingIterator(
new ArrayIterator( $arr ),
CachingIterator::FULL_CACHE
); // Check the validity of ArrayIterator while ( $cachIt ->valid()) {
$cachIt ->next();
} // Move to start position $cachIt -> rewind ();
// Display the current element echo $cachIt ->current();
?> |
Output:
for
Reference: https://www.php.net/manual/en/cachingiterator.rewind.php