- array_walk: documentation ( source)
<?php
$doc = new DomDocument('1.0', 'UTF-8');
$doc->formatOutput = true;
$iterator = new SplTempFileObject();
$iterator->setFlags(SplTempFileObject::READ_CSV);
$iterator->fputcsv(array('john', 'doe', 'john.doe@example.com'));
$iterator->fputcsv(array('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) {
$cell = $doc->createElement('td');
$cell->appendChild($doc->createTextNode($value));
$item->appendChild($cell);
});
$root->appendChild($item);
}
$doc->appendChild($root);
$doc->documentElement->setAttribute('class', 'foo');
echo $doc->saveHTML($doc->documentElement);