ArrayObject getIterator () Function in PHP

PHP

Syntax :
ArrayIterator getIterator() 
Parameters : This function takes no parameters.Return Value : This function returns an Iterator from an ArrayObject instance.The following programs illustrate the above function:Program 1:
// PHP illustration program
// getIterator() function $arr = array ( "a" = > "engineer" , "b" = > "are" , "c" = > "awesome" );  
// Create array object $arrObject = new ArrayObject ( $arr );  
// Create an iterator $itr = $arrObject -> getIterator();  
// Using an iterator to traverse an array while ( $itr -> valid()) { echo $itr -> key(). '= > ' . $itr -> current(). "" ; $itr -> next(); }  
?>
Exit:
a = > engineer b = > are c = > awesome 
Program 2 :
// PHP illustration program
// getIterator() function $arr = array ( "a" = > "Welcome" , "b" = > "2" , "d" = > "GFG" );  
// Create array object $arrObject = new ArrayObject ( $arr );  
// Create an iterator $itr = $arrObject -> getIterator();  
// Using an iterator to traverse an array while ( $itr -> valid()) { echo $itr -> key(). '= > ' . $itr -> current(). "" ; $itr -> next(); }  
?>
Exit:
a = > Welcome b = > 2 d = > GFG 
Link : http: // php .net / manual / en / arrayobject.getiterator.php



Get Solution for free from DataCamp guru