所以我有两个CSV:InputCSV和OutputCSV
InputCSV.csv
name col1 col2 col3 col4 col5
john qOJY OHXl vIOH Tdnm Z7OH
greg YRc1 hyFB pW8m 5LSE Yo4r
saly Dy4o 51Ui tuKI 02VQ RVgB
OutputCSV.csv
name url
john site.com/JcRIeyFCEl.mp4
greg site.com/TTwF4Cue2B.mp4
saly site.com/ouroANTtAC.mp4
所以在processor.php中,我有
if (($csvFile = fopen("InputCSV.csv", "r")) !== false && ($resultCsv = fopen("OutputCSV.csv", 'w')) !== false) {
while (($data = fgetcsv($csvFile)) !== false) {
// do stuff to generate $thumbfile
// this bit needs to use parts of $data like $data[3], $data[5] to generate $thumbfile
// which in the end should look like $thumbfile = 'site.com/rAndOmsTRing.jpg';
}
$outputData = fgetcsv($resultCsv);
$outputData[] .= $thumbfile_url; //Append to end of line. is this what I'm doing wrong?
fputcsv($resultCsv, $outputData);
我需要做的是向OutputCSV.csv添加一个新列,并在每行/每行的末尾附加$ thumbfile。目前,它的作用是删除两个现有列,并仅将$ thumbfile写为一列。
在此脚本运行后,OutputCSV.csv需要如下所示: OutputCSV.csv
name url thumbfile
john site.com/JcRIeyFCEl.mp4 site.com/snTflxaqNI.jpg
greg site.com/TTwF4Cue2B.mp4 site.com/ELrg6vwKEr.jpg
saly site.com/ouroANTtAC.mp4 site.com/xTjfqCEoIZ.jpg
注意:我使用的实际csv没有标题。我只是把它们放在那里,以帮助更容易阅读。
答案 0 :(得分:0)
要制作你需要的东西,你需要将OutputCSV.csv的数据保存在数组中,然后在同一个数组中添加你的$ thumbfile。
这里有一个例子,我用你的csv文件用“,”分隔。
if (($csvFile = fopen("InputCSV.csv", "r")) !== false && ($resultCsv = fopen("OutputCSV.csv", 'r')) !== false) {
while (($data = fgetcsv($csvFile,4096,",")) !== false) {
// do stuff to generate $thumbfile
$thumbfile[]=$data[3].$data[5].".jpg";
}
$x=0;
while (($dataoutput = fgetcsv($resultCsv,4096,",")) !== false) {
$dataotputcsv[$x]=$dataoutput; //here is [0]=> "john" [1]=> "site.com/JcRIeyFCEl.mp4"
array_push($dataotputcsv[$x], $thumbfile[$x]); //here i add the $thumbfile
//and now is [0]=> "john" [1]=> "site.com/JcRIeyFCEl.mp4" [2]=> "vIOHZ7OH.jpg"
$x++;
}
$file = fopen("OutputCSV.csv", 'w');
foreach($dataotputcsv as $string) {
fputcsv($file, $string); //save all lines to file
}
}