[Solved] How to Merge CSV Files in PHP, Line by Line


Try this php code (gets the lines with file()).:

<?php
$csv1 = file('csv1.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$csv2 = file('csv2.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$csv3 = file('csv3.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$lines = max(count($csv1), count($csv2), count($csv3));
$finalcsv = array();
for($i=0; $i<$lines; $i++) {
  if(isset($csv1[$i])) $finalcsv[] = $csv1[$i];
  if(isset($csv2[$i])) $finalcsv[] = $csv2[$i];
  if(isset($csv3[$i])) $finalcsv[] = $csv3[$i];
}

file_put_contents('final_data.csv', implode(PHP_EOL, $finalcsv));

solved How to Merge CSV Files in PHP, Line by Line