php 如何将 DOMNodeList 对象转换为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15807314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:00:35  来源:igfitidea点击:

How to convert DOMNodeList object into array

php

提问by Oto Shavadze

I have this code:

我有这个代码:

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

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

现在,$names是 DOMNodeList 对象,我需要把这个对象转换成一个数组,

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

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

上面的代码不起作用并返回一个空数组,为什么?

采纳答案by álvaro González

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

为什么(数组)不起作用?因为DOMNodeList对象只有一个属性length,并且它是整数类型:

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 DOMNodeListimplements the Traversableinterface, it's fairly easy to create the array yourself:

由于DOMNodeList实现了Traversable接口,因此自己创建数组相当容易:

$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 DOMNodeListis not a user object created with PHP code but a builtin object defined in C, so different rules apply.

解释可能更多的是,DOMNodeList它不是用 PHP 代码创建的用户对象,而是用 C 定义的内置对象,因此适用不同的规则。

回答by Blar

Use iterator_to_array() to convert a DomNodeList to an Array

使用 iterator_to_array() 将 DomNodeList 转换为数组

$document = new DomDocument();
$document->load('test.xrds');
$nodeList = $document->getElementsByTagName('Type');
$array = iterator_to_array($nodeList);

回答by complex857

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 textContentproperty.

这样它们仍然是 DOMNode 实例,您可能想要获取它们的textContent属性。

回答by Puzzled Boy

$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;
} 

回答by Will

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

假设您想要实际值而不是创建 DOM 节点数组:

foreach($nodelist as $node) {
    $values[] = $node->nodeValue;
}