Hello 我正在制作RSS阅读器,并且正在使用DOM。 现在我卡住了,试图颠倒DOMNodeList中的项目顺序。 我可以使用2个循环 - 一个将其设置为数组,一个用于 rsort()。 有什么办法可以反转DOMNodeList中的顺序,还是必须完成数组方式?
Hello I'm making RSS reader and I'm using DOM. Now I stuck, trying to reverse the order of the items in the DOMNodeList. I can make it with 2 cycles - one to make it as array and one for rsort(). Is there any way to reverse the order in the DOMNodeList or must be done with "the array way"?
推荐答案没有方法来反转DOMNodeList。
There is no method for reversing a DOMNodeList.
但是,您可以保持原样,如果需要,请从头开始执行。
But you can keep it as it is, and if you need it, walk through it from the end to the start.
示例:
<?php $doc=new DOMDocument; $doc->loadXML(' <div> <span>1 <span>2 <span>3 </span> </span> </span> </div>'); $nodeList=$doc->getElementsByTagName('span'); for($n=$nodeList->length-1;$n>=0;--$n) { echo $nodeList->item($n)->firstChild->data;//returns 321 } ?>只需使用 NodeList->长度指向NodeList的末尾,然后递减索引并访问 NodeList-> item(index)
Just point at the end of the NodeList using NodeList->length, then decrement the index and access NodeList->item(index)
更多推荐
DOMNodeList中的项目的反转顺序
发布评论