Friday, January 16, 2026
HomeLanguagesPHP | CachingIterator getCache() Function

PHP | CachingIterator getCache() Function

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
    
// 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
    
// 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

RELATED ARTICLES

Most Popular

Dominic
32474 POSTS0 COMMENTS
Milvus
117 POSTS0 COMMENTS
Nango Kala
6846 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12062 POSTS0 COMMENTS
Shaida Kate Naidoo
6985 POSTS0 COMMENTS
Ted Musemwa
7219 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6911 POSTS0 COMMENTS