$array = array( 'fruit' => 'apple', 'colors' => array('red', 'green', 'blue') ); $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array)); while($iterator->valid()) { echo $iterator->key(). ": ".$iterator->current()."\n"; $iterator->next(); }
$xml = simplexml_load_file('example.xml'); $iterator = new RecursiveIteratorIterator($xml, RecursiveIteratorIterator::SELF_FIRST); foreach($iterator as $element) { echo $element->getName().": ".$element."\n"; }This code uses the RecursiveIteratorIterator to parse an XML document and print each element name and value. "self-first" mode specifies that the XML elements themselves should be visited before their children. The package library used by RecursiveIteratorIterator is the built-in SPL (Standard PHP Library) package. It provides a set of interfaces and classes for working with data structures such as arrays, iterators, and SPL-specific exceptions.