<?php
$doc = new DomDocument('1.0', 'UTF-8');
$doc->formatOutput = true;
$iterator = new ArrayIterator([
['john', 'doe', 'john.doe@example.com'],
['jane', 'doe', 'jane.doe@example.com'],
]);
$root = $doc->createElement('table');
foreach ($iterator as $row) {
$item = $doc->createElement('tr');
array_walk($row, function ($value) use (&$item, $doc) {
$content = $doc->createTextNode($value);
$cell = $doc->createElement('td');
$cell->appendChild($content);
$item->appendChild($cell);
});
$root->appendChild($item);
}
$doc->appendChild($root);
$doc->documentElement->setAttribute('class', 'foo');
echo '<pre>', htmlspecialchars($doc->saveHTML($doc->documentElement));
preferences:
65.97 ms | 402 KiB | 5 Q