PHP | AppendIterator getIteratorIndex() Function

The AppendIterator::getIteratorIndex() function is an inbuilt function in PHP which is used to get the index of the current inner iterator.
Syntax:
int AppendIterator::getIteratorIndex( void )
Parameters: This function does not accept any parameters.
Return Value: This function returns an integer value which is the zero-based index of the current inner iterator.
Below programs illustrate the AppendIterator::getIteratorIndex() function in PHP:
Program 1:
<?php   // Declare an ArrayIterator $arr1 = new ArrayIterator(array("Geeks", "for", "Geeks")); $arr2 = new ArrayIterator(array("Computer", "Science", "Portal"));   // Create a new AppendIterator $itr = new AppendIterator; $itr->append($arr1); $itr->append($arr2);   // Display the elements foreach ($itr as $key => $val) {     echo "Iterator Index: " . $itr->getIteratorIndex()         . " Key : " . $key . " Value: " . $val . "\n"; }   ?> |
Output:
Iterator Index: 0 Key : 0 Value: Geeks Iterator Index: 0 Key : 1 Value: for Iterator Index: 0 Key : 2 Value: Geeks Iterator Index: 1 Key : 0 Value: Computer Iterator Index: 1 Key : 1 Value: Science Iterator Index: 1 Key : 2 Value: Portal
Program 2:
<?php   // Declare an ArrayIterator $arr1 = new ArrayIterator(     array(         "a" => "Geeks",         "b" => "for",         "c" => "Geeks"    ) );   $arr2 = new ArrayIterator(     array(         "x" => "Computer",         "y" => "Science",         "z" => "Portal"    ) );   // Create a new AppendIterator $itr = new AppendIterator; $itr->append($arr1); $itr->append($arr2);   // Display the elements foreach ($itr as $key => $val) {     echo "Iterator Index: " . $itr->getIteratorIndex()         . " Key : " . $key . " Value: " . $val . "\n"; }   ?> |
Output:
Iterator Index: 0 Key : a Value: Geeks Iterator Index: 0 Key : b Value: for Iterator Index: 0 Key : c Value: Geeks Iterator Index: 1 Key : x Value: Computer Iterator Index: 1 Key : y Value: Science Iterator Index: 1 Key : z Value: Portal
Reference: https://www.php.net/manual/en/appenditerator.getiteratorindex.php



