How to convert DOMNodeList object into array

后端 未结 5 525
春和景丽
春和景丽 2021-01-03 22:58

I have this code:

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

N

相关标签:
5条回答
  • 2021-01-03 23:21
    $dom = new DOMDocument();
    $dom->load('file.xml');
    $names = $dom->getElementsByTagName('name');
    $headlines = array();
    
    foreach($names as $name) {
        $headline = array();
        if($name->childNodes->length) {
            foreach($name->childNodes as $i) {
                $headline[$i->nodeName] = $i->nodeValue;
            }
        }
    
        $headlines[] = $headline;
    } 
    
    0 讨论(0)
  • 2021-01-03 23:28

    Assuming you want the actual values and not to create an array of DOM nodes:

    foreach($nodelist as $node) {
        $values[] = $node->nodeValue;
    }
    
    0 讨论(0)
  • 2021-01-03 23:29

    Just iterate trough the elements and put them into an array:

    $names_array = array();
    foreach ($names as $name) {
        $names_array[] = $name; // this is a DOMNode instance
        // you might want to have the textContent of them like this
        $names_array[] = $name->textContent;
    }
    

    This way they still will be DOMNode instances, you might want to get their textContent property.

    0 讨论(0)
  • 2021-01-03 23:38

    Why doesn't (array) work? Because a DOMNodeList object has only one property, length, and it's of integer type:

    If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side.

    Since a DOMNodeList implements the Traversable interface, it's fairly easy to create the array yourself:

    $array = array();
    foreach($names as $node){
        $array[] = $node;
    }
    

    Edit: I've removed the initial explanation because it doesn't apply here. What the manual means is that properties with numeric names are passed to the array but cannot be read:

    <?php
    
    class Foo{
        public function __construct(){
            $this->{123} = 456;
        }
    }
    
    $array = (array)new Foo;
    var_dump($array);
    var_dump($array['123']);
    
    array(1) {
      ["123"]=>
      int(456)
    }
    
    Notice: Undefined offset: 123 in D:\tmp\borrame.php on line 11
    NULL
    

    The explanation is probably more on the line that DOMNodeList is not a user object created with PHP code but a builtin object defined in C, so different rules apply.

    0 讨论(0)
  • 2021-01-03 23:38

    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);
    
    0 讨论(0)
提交回复
热议问题