Open In App

PHP | CachingIterator rewind() Function

Last Updated : 26 Nov, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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
<?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
<?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

Next Article

Similar Reads