Edit report at https://bugs.php.net/bug.php?id=38489&edit=1
ID: 38489 Comment by: hanskrentel at yahoo dot de Reported by: mmcintyre at squiz dot net Summary: DOMNodeList should implement Traversable Status: Closed Type: Feature/Change Request Package: DOM XML related Operating System: * PHP Version: 5.1.5 Assigned To: jpauli Block user comment: N Private report: N New Comment: iterator_to_array() accepts any terversable, therefore there is no need to wrap the DOMNodeList into an IteratorIterator first, it can be converted to array *directly* <?php $xml = '<queries><query attr1="value"></query></queries>'; $doc = new DOMDocument; $doc->loadXML($xml); $queries = $doc->getElementsByTagName('queries'); $nodes = iterator_to_array($queries); ?> The key used per each node is some kind of ID (positive, long integer) and it seems to be unique. Using an array can be useful because a DOMNodeList automatically re-orders if nodes therein are deleted. This is not the case with the array (naturally). Previous Comments: ------------------------------------------------------------------------ [2012-01-10 16:42:53] jpa...@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php DomNodeListe implements Traversable. You can convert a Traversable object to an Iterator using the IteratorIterator class. <?php $xml = '<queries><query attr1="value"></query></queries>'; $doc = new DOMDocument; $doc->loadXML($xml); $queries = $doc->getElementsByTagName('queries'); $nodes = iterator_to_array(new IteratorIterator($queries)); ?> ------------------------------------------------------------------------ [2007-01-17 15:11:55] jules_papillon_fh at yahoo dot de This bug exists further reproducibly on PHP5.2 ------------------------------------------------------------------------ [2007-01-17 15:09:11] jules_papillon_fh at yahoo dot de Another Code to reproduce the Bug: ---------------------------------- $dom = new DOMDocument('1.0', 'ISO-8859-1'); $dom->load('file.xml'); $iterator = new RecursiveIteratorIterator($dom->childNodes, RecursiveIteratorIterator::SELF_FIRST); foreach($iterator as $name => $element) { print $name . "\n"; } Expected result: ---------------- A recursive List of all Elements Actual result: -------------- "Catchable fatal error: Argument 1 passed to RecursiveIteratorIterator::__construct() must implement interface Traversable, instance of DOMNodeList given, called in [ ] and defined in [ ]" ------------------------------------------------------------------------ [2006-08-18 06:37:22] mmcintyre at squiz dot net Description: ------------ Currently, a DOMNodeList object can be traversed using a foreach loop, but it cannot be converted to an array using iterator_to_array(), as it produces an error "Warning: iterator_to_array() expects parameter 1 to be Traversable, object given" Reproduce code: --------------- $xml = '<queries><query attr1="value"></query></queries>'; $doc = new DOMDocument; $doc->loadXML($xml); $queries = $doc->getElementsByTagName('queries'); $nodes = iterator_to_array($queries); Expected result: ---------------- The nodes in the NodeList are returned as an array. Actual result: -------------- "Warning: iterator_to_array() expects parameter 1 to be Traversable, object given" ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=38489&edit=1