Saturday, November 22, 2025
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
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6784 POSTS0 COMMENTS
Nicole Veronica
11931 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11999 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6863 POSTS0 COMMENTS
Umr Jansen
6848 POSTS0 COMMENTS