PHP | AppendIterator getIteratorIndex() Function
Last Updated :
20 Nov, 2019
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
$arr1 = new ArrayIterator( array ( "Geeks" , "for" , "Geeks" ));
$arr2 = new ArrayIterator( array ( "Computer" , "Science" , "Portal" ));
$itr = new AppendIterator;
$itr ->append( $arr1 );
$itr ->append( $arr2 );
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
$arr1 = new ArrayIterator(
array (
"a" => "Geeks" ,
"b" => "for" ,
"c" => "Geeks"
)
);
$arr2 = new ArrayIterator(
array (
"x" => "Computer" ,
"y" => "Science" ,
"z" => "Portal"
)
);
$itr = new AppendIterator;
$itr ->append( $arr1 );
$itr ->append( $arr2 );
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
Share your thoughts in the comments
Please Login to comment...