拆分一段字符串为CSV不同的列

比如就数据是:

1,"Season:Spring, Summer, Fall;Fabric:Elastic Woven Satin;Neckline:One-Shoulder;Silhouette:Trumpet/Mermaid;Sleeve:Sleeveless;Embellishment:Ruffles;Waist:Natural;Hemline/Train:Sweep/Brush Train;Body Shape:Hourglass;Shown Color:Red;Package Weight:2500.0000"
想改为
1,"Spring, Summer, Fall","Elastic Woven Satin",One-Shoulder,Trumpet/Mermaid,Sleeveless,Ruffles,Natural,"Sweep/Brush Train",Hourglass,Red,2500.0000,,,,,,,,,
代为为:
$columnSource = array('sku', 'Season', 'Fabric', 'Neckline', 'Silhouette', 'Sleeve', 'Embellishment', 'Waist', 'Hemline/Train', 'Body Shape', 'Shown Color', 'Package Weight', 'Skirt Fabric', 'Sash Fabric', 'Decoration', 'Heel Height', 'Heel Height Range', 'Lining Material', 'Outsole Material', 'Upper Material', 'Platform Height');
$columnTarget = array_map('is_array', array_flip($columnSource));

$target = fopen('target.csv', 'w');
fputcsv($target, $columnSource);

if (($source = fopen('source.csv', 'r')) !== false) {
	while (($row = fgetcsv($source, 10000, ',')) !== false) {
		$data = $columnTarget;
		$data['sku'] = $row[0];
		
		$group = array_map('trim', explode(';', $row[1]));
		foreach($group as $value){
			$v = array_map('trim', explode(':', $value));
			$data[$v[0]] = $v[1];
		}
		fputcsv($target, $data);
	}
	fclose($source);
}
exit();

你可能感兴趣的:(CSV拆分列)