用php编辑csv

我有大约700行的csv。它包括如下行:


92265R-1,1

92526R-3,0

.

.

.

现在,我的目标是将每一行复制到新的csv,其中每行以_x结尾复制5次。因此最终的csv将如下所示:


92265R-1_1,1

92265R-1_2,1

92265R-1_3,1

92265R-1_4,1

92265R-1_5,1

92526R-3_1,0

92526R-3_2,0

92526R-3_3,0

92526R-3_4,0

92526R-3_5,0

.

.

我被困在这里


有只小跳蛙
浏览 120回答 1
1回答

拉风的咖菲猫

只需尝试一下$newArray = [];if (($handle = fopen("test.csv", "r")) !== FALSE) {while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {&nbsp; for($i=1 ; $i <=5 ; $i++){&nbsp; &nbsp; $newArray[] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $row[0].'_'.$i,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $row[1]&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp;}&nbsp; }&nbsp; fclose($handle);}$fp = fopen('new.csv', 'w');foreach ($newArray as $fields) {&nbsp; &nbsp; fputcsv($fp, $fields);}fclose($fp);
打开App,查看更多内容
随时随地看视频慕课网APP