例如:csv
有两列。结构是:它只有标题。
sku name
现在,有一个 foreach 循环:
foreach ($collection as $product_all)
{
$sku = $product_all['sku'];
$product_id = Mage::getModel('catalog/product')->getIdBySku($sku);
$product = Mage::getModel('catalog/product');
$product ->load($product_id);
$name = $product['name'];
}
如果有 8000 行,我想将$sku
和放入 csv 文件中。$name
我该怎么办?我使用以下代码。但它不起作用。如何纠正它?谢谢你。
foreach ($collection as $product_all)
{
$sku = $product_all['sku'];
$product_id = Mage::getModel('catalog/product')->getIdBySku($sku);
$product = Mage::getModel('catalog/product');
$product ->load($product_id);
$name = $product['name'];
$newCsvData = array();
if (($handle = fopen("test.csv", "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$data[0] = $sku;
$data[2] = $name;
$newCsvData[] = $data;
}
fclose($handle);
}
$handle = fopen('test.csv', 'w');
foreach ($newCsvData as $line)
{
fputcsv($handle, $line);
}
fclose($handle);
}