成功的代码将
启动CSV文件的例子。
产品名称,产品类型,尺寸,类别,子类别。
12桶啤酒,12桶啤酒,比尔森啤酒
百威啤酒12桶,啤酒,12桶,,比尔森啤酒
预计CSV文件结束。
product_name,producttype,size,category,sub_category。
12桶啤酒,12桶啤酒,12桶啤酒;12桶啤酒;比尔森啤酒,比尔森啤酒
百威啤酒12桶,啤酒,12桶,啤酒;12桶;比尔森,比尔森啤酒。
这是我找到的解决方案。可能不是最优雅的一个。
<?php
//Brand_Name Brand_Description SKUNumber UPC product_type CATEGORY SUB-CATEGORY BEER TYPE BEER STYLE PRODUCER price_regular Size Web Active Quantity_Available
$columns = ["Brand_Name","Brand_Description","SKUNumber","UPC","product_type","CATEGORY","SUB-CATEGORY","BEER TYPE","BEER STYLE","PRODUCER","price_regular","Size","Web Active","Quantity_Available"];
//$columns = ["sku","category","description","image","small_image","thumbnail", "price","color"];
$file = fopen("Products_In.csv", "r"); //Open the old file for reading
$newFile = fopen("Products_Out.csv", "w"); //Create a new file for writing
while (($data = fgetcsv($file)) !== FALSE) {
$row = array_combine($columns, $data);
if(!empty($row['BEER TYPE'])){
$category = "{$row['product_type']};{$row['Size']};{$row['BEER TYPE']}";
} else {
$category = "{$row['product_type']};{$row['Size']}";
}
$row['SUB-CATEGORY'] = $category;
fputcsv($newFile, array_values($row)); //write data into new file
}
fclose($file);
fclose($newFile);
echo "<h1>Categories updated</h1>";
?>