How to convert DOMNodeList object into array

后端 未结 5 559
春和景丽
春和景丽 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: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.

提交回复
热议问题