PHP | CachingIterator getCache() Function

Last Updated : 20 Nov, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
The CachingIterator::getCache() function is an inbuilt function in PHP which is used to retrieve the contents of the cache. Syntax:
array CachingIterator::getCache( void )
Parameters: This function does not accept any parameters. Return Value: This function returns an array containing the cache items. Below programs illustrate the CachingIterator::getCache() function in PHP: Program 1: php
<?php
  
// Declare an array
$arr = array('G', 'e', 'e', 'k', 's');
  
// Create a new CachingIterator
$cachIt = new CachingIterator(
    new ArrayIterator($arr), 
    CachingIterator::FULL_CACHE
);
 
// Move to next position
$cachIt->next();

// Display the content of cache
var_dump($cachIt->getCache());

// Move to next position
$cachIt->next();

// Display the content of cache
var_dump($cachIt->getCache());
  
?>
Output:
array(1) {
  [0]=>
  string(1) "G"
}
array(2) {
  [0]=>
  string(1) "G"
  [1]=>
  string(1) "e"
}
Program 2: php
<?php
  
// Declare an ArrayIterator
$arr = array(
    "a" => "Geeks",
    "b" => "for",
    "c" => "Geeks",
    "d" => "Computer",
    "e" => "Science",
    "f" => "Portal"
);

// Create a new CachingIterator
$cachIt = new CachingIterator(
    new ArrayIterator($arr), 
    CachingIterator::FULL_CACHE
);

// Move to next position
$cachIt->next();
$cachIt->next();
$cachIt->next();

// Display the content of cache
var_dump($cachIt->getCache());

// Move to next position
$cachIt->next();

// Display the content of cache
var_dump($cachIt->getCache());
  
?>
Output:
array(3) {
  ["a"]=>
  string(5) "Geeks"
  ["b"]=>
  string(3) "for"
  ["c"]=>
  string(5) "Geeks"
}
array(4) {
  ["a"]=>
  string(5) "Geeks"
  ["b"]=>
  string(3) "for"
  ["c"]=>
  string(5) "Geeks"
  ["d"]=>
  string(8) "Computer"
}
Reference: https://www.php.net/manual/en/cachingiterator.getcache.php

Next Article

Similar Reads