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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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"
}
  
?>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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"
}
  
?>

chevron_right


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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.