phparraysdom-node

php domnodelist with array chunk


I would like to use an an array chunk function with a domnodelist. This causes an error as the domnodelist is not a standard php array. Here is my code:

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

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

Is there a way to make this work?

Thank you,

Bill


Solution

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