php – 致命错误:无法使用DOMNodeList类型的对象作为数组

前端之家收集整理的这篇文章主要介绍了php – 致命错误:无法使用DOMNodeList类型的对象作为数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我花了很多时间编写一个执行某项任务的脚本,当我在我的本地机器上测试它时工作正常,但是当我将它上传到我的主机时它会给我这个错误
Fatal error: Cannot use object of type DOMNodeList as array

这是脚本所做的一个示例

$xml = new DOMDocument();
$xml->loadHTML($html);

$xpath = new DOMXPath($xml);
$table =$xpath->query("//*[@style='background: #aaaaaa']")->item(0);



$rows = $table->getElementsByTagName("tr");

foreach ($rows as $row) {
    if($row->getAttribute('align') === 'center') {
  $cells = $row -> getElementsByTagName('td');

  // I GET THE ERROR FROM THIS LINE
  $add = MysqL_escape_string(utf8_decode($cells[0]->nodeValue));

  Some logic 

  }

就像我说它在我的本地机器上工作正常,当我在我的托管上运行时我得到错误

我使用此代码获取加载的扩展,因为我认为问题可能来自那里

print_r(get_loaded_extensions());

这是我的机器的结果

Array
(
    [0] => Core
    [1] => bcmath
    [2] => calendar
    [3] => ctype
    [4] => date
    [5] => ereg
    [6] => filter
    [7] => ftp
    [8] => hash
    [9] => iconv
    [10] => json
    [11] => mcrypt
    [12] => SPL
    [13] => odbc
    [14] => pcre
    [15] => Reflection
    [16] => session
    [17] => standard
    [18] => MysqLnd
    [19] => tokenizer
    [20] => zip
    [21] => zlib
    [22] => libxml
    [23] => dom
    [24] => PDO
    [25] => bz2
    [26] => SimpleXML
    [27] => wddx
    [28] => xml
    [29] => xmlreader
    [30] => xmlwriter
    [31] => apache2handler
    [32] => openssl
    [33] => curl
    [34] => mbstring
    [35] => exif
    [36] => gd
    [37] => gettext
    [38] => intl
    [39] => MysqL
    [40] => MysqLi
    [41] => Phar
    [42] => pdo_MysqL
    [43] => pdo_sqlite
    [44] => soap
    [45] => sockets
    [46] => sqlite3
    [47] => xmlrpc
    [48] => xsl
    [49] => mhash
)

从我的托管

Array
(
    [0] => Core
    [1] => date
    [2] => ereg
    [3] => libxml
    [4] => openssl
    [5] => pcre
    [6] => sqlite3
    [7] => zlib
    [8] => bcmath
    [9] => bz2
    [10] => calendar
    [11] => ctype
    [12] => curl
    [13] => dom
    [14] => hash
    [15] => fileinfo
    [16] => filter
    [17] => ftp
    [18] => gd
    [19] => gettext
    [20] => SPL
    [21] => iconv
    [22] => session
    [23] => intl
    [24] => json
    [25] => mbstring
    [26] => mcrypt
    [27] => standard
    [28] => MysqL
    [29] => MysqLi
    [30] => pgsql
    [31] => MysqLnd
    [32] => Phar
    [33] => posix
    [34] => pspell
    [35] => Reflection
    [36] => imap
    [37] => SimpleXML
    [38] => soap
    [39] => sockets
    [40] => exif
    [41] => tidy
    [42] => tokenizer
    [43] => xml
    [44] => xmlreader
    [45] => xmlrpc
    [46] => xmlwriter
    [47] => xsl
    [48] => zip
    [49] => cgi-fcgi
    [50] => PDO
    [51] => pdo_sqlite
    [52] => pdo_MysqL
    [53] => ionCube Loader
    [54] => Zend Guard Loader
)

我不知道为什么我会收到错误

请阅读文档:

http://php.net/manual/en/domdocument.getelementsbytagname.php

This function returns a new instance of class DOMNodeList containing
all the elements with a given local tag name.

它是一个对象,而不是一个数组,这意味着你不能使用$cells [0].

猜你在找的PHP相关文章