|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-08-18 06:37 UTC] 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"
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 16:00:01 2025 UTC |
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 [?]"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).