php domnodelist with array chunk

时间:2012-09-19 02:25:50

标签: php arrays dom-node

我想使用带有domnodelist的数组块函数。这会导致错误,因为domnodelist不是标准的php数组。这是我的代码:

 foreach (array_chunk($nodeListArray, 2) as $chunk) { //each 2 make up 1 table

        $tableHTML='';
        foreach ($chunk as $key => $node) {
           $tableHTML.= $doc->saveHTML($node);
        }

有没有办法让这项工作?

谢谢,

比尔

2 个答案:

答案 0 :(得分:1)

$tableHTML = '';
$tables = array();
foreach ($nodeListArray as $i => $node) {
  $tableHTML .= $doc->saveHTML($node);
  if ($i % 2 === 1) {
     $tables[] = $tableHTML;
     $tableHTML = '';
  }
}

答案 1 :(得分:0)

尝试array_chunk(iterator_to_array($domNodeList, 2))从迭代器

创建数组