|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #38489 DOMNodeList should implement Traversable
Submitted: 2006-08-18 06:37 UTC Modified: 2012-01-10 16:42 UTC
Avg. Score:4.4 ± 0.8
Reproduced:4 of 5 (80.0%)
Same Version:1 (25.0%)
Same OS:2 (50.0%)
From: mmcintyre at squiz dot net Assigned: jpauli (profile)
Status: Closed Package: DOM XML related
PHP Version: 5.1.5 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: mmcintyre at squiz dot net
New email:
PHP Version: OS:


 [2006-08-18 06:37 UTC] mmcintyre at squiz dot net
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;

$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"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-01-17 15:09 UTC] jules_papillon_fh at yahoo dot de
Another Code to reproduce the Bug:
$dom = new DOMDocument('1.0', 'ISO-8859-1');
$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 [?]"
 [2007-01-17 15:11 UTC] jules_papillon_fh at yahoo dot de
This bug exists further reproducibly on PHP5.2
 [2011-02-21 21:23 UTC]
-Package: Feature/Change Request +Package: DOM XML related
 [2012-01-10 16:42 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

DomNodeListe implements Traversable. You can convert a Traversable object to an 
Iterator using the IteratorIterator 

$xml = '<queries><query attr1="value"></query></queries>';

$doc = new DOMDocument;

$queries = $doc->getElementsByTagName('queries');
$nodes   = iterator_to_array(new IteratorIterator($queries));
 [2012-01-10 16:42 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: jpauli
 [2013-06-11 08:38 UTC] hanskrentel at yahoo dot de
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 

$xml = '<queries><query attr1="value"></query></queries>';

$doc = new DOMDocument;

$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).
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Fri Sep 22 15:01:24 2023 UTC