I'm trying to write the tab-delimited content of a variable to XML like this:
$tsvData = str_getcsv($input, "\t");
foreach($tsvData as $line => $row) {
if($line > 0) {
$xmlWriter->writeElement('NAME', $row[0]);
$xmlWriter->writeElement('CAKE', $row[1]);
$xmlWriter->writeElement('BODYPART', $row[2]);
}
}
But it's only writing one character per XML tag instead of everything between each tab. When I use SplFileObject, geting the same tsv data but from a file, it works. What am I doing wrong with the str_getcsv function?
Thanks
The
str_getcsv()
function returns a 1-dimensional array, but you're treating it like it's returning a 2-dimensional array.Edit:
To clarify,
str_getcsv()
has no concept of "lines". Instead of doing this:Thinking that you'll get an array of lines, each one containing an array of columns, you have to do something like this: