你好,我有这段代码
<?php
error_reporting(0);
function csv_to_array($filename='', $delimiter=';')
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
$csvFile = "example.csv";
$csv = csv_to_array($csvFile);
echo '<pre>';
print_r($csv);
echo '</pre>';
$xml = new DOMDocument("1.0");
$root = $xml->createElement("CATEGORY");
$domAttribute = $xml->createAttribute('name');
// Value for the created attribute
$domAttribute->value = 'Clear Skin 100ml Massage Oil 100ml-massage-oils';
// Don't forget to append it to the element
$root->appendChild($domAttribute);
$xml->appendChild($root);
foreach ($csv as $key => $value) {
$book = $xml->createElement("PRODUCT");
foreach ($value as $key => $value) {
echo $key. " ==> " . $value."<br>";
$key = $xml->createElement($key);
$value = $xml->createTextNode($value);
$key->appendChild($value);
$book->appendChild($key);
}
$root->appendChild($book);
}
$xml->formatOutput = true;
echo "<xmp>". $xml->saveXML() ."</xmp>";
$xml->save("final.xml") or die("Error");
我创建了这个脚本来从csv文件中获取数据并存储在xml文件中。但我的输出错误
我的输出:
Category ==> SMALL HOME APPLIANCES
Market ==> Breakfast
Segment ==> Hot beverage systems
PIXpro SKU ==> 19392107
它不会打印csv文件中的所有数据,但是当我删除
时 $key = $xml->createElement($key);
$value = $xml->createTextNode($value);
$key->appendChild($value);
$book->appendChild($key);
此代码然后我得到所有数据回声就好了可以帮助我