Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert DOMNodeList object into array

Tags:

I have this code:

     $dom = new DOMDocument();      $dom->load('file.xml');      $names = $dom->getElementsByTagName('name'); 

Now, $names is DOMNodeList object, I need to convert this object into an array,

     $names = (array)$names;      var_dump($names);  // empty array 

The above code does not work and returns an empty array, why?

like image 805
Oto Shavadze Avatar asked Apr 04 '13 09:04

Oto Shavadze


1 Answers

Use iterator_to_array() to convert a DomNodeList to an Array

$document = new DomDocument(); $document->load('test.xrds'); $nodeList = $document->getElementsByTagName('Type'); $array = iterator_to_array($nodeList); 
like image 64
Blar Avatar answered Oct 02 '22 08:10

Blar