Php 将动态生成的字符串追加到CSV行的末尾
所以我有两个CSV:InputCSV和OutputCSV InputCSV.csvPhp 将动态生成的字符串追加到CSV行的末尾,php,csv,append,fputcsv,Php,Csv,Append,Fputcsv,所以我有两个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 name
name col1 col2 col3 col4 col5
john qOJY OHXl vIOH Tdnm Z7OH
greg YRc1 hyFB pW8m 5LSE Yo4r
saly Dy4o 51Ui tuKI 02VQ RVgB
name url
john site.com/JcRIeyFCEl.mp4
greg site.com/TTwF4Cue2B.mp4
saly site.com/ouroANTtAC.mp4
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
OutputCSV.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
name url
john site.com/JcRIeyFCEl.mp4
greg site.com/TTwF4Cue2B.mp4
saly site.com/ouroANTtAC.mp4
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
所以在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 col1 col2 col3 col4 col5
john qOJY OHXl vIOH Tdnm Z7OH
greg YRc1 hyFB pW8m 5LSE Yo4r
saly Dy4o 51Ui tuKI 02VQ RVgB
name url
john site.com/JcRIeyFCEl.mp4
greg site.com/TTwF4Cue2B.mp4
saly site.com/ouroANTtAC.mp4
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没有标题。我把它们放在那里只是为了方便阅读。要制作您需要的内容,您需要将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
}
}