Friday 15 February 2013

parsing - How to Merge CSV Files in PHP, Line by Line -


I am trying to merge many CSV files together in PHP. I have already merged them into full files using PHP thanks to the thread (though), though I want to try mixing files together by personalized line

I have a line from 1 CSV 1 , from line 1 to CSV 2 , then I would like to read more, then go to line 2, 3, etc. - Cycling between files in the array with each loop I have not done much work to parse the files in PHP, so I'm not sure where to start it.

If the above is not clear, I am trying to fulfill it: CSV1 - Line 1

CSV2 - Line 1 < P> CSV3 - Line 1

CSV1 - Line 2

CSV2 - Line2

CSV3 - Line 2

CSV1 - Line 3

CSV2 - Line 3

CSV3 - Line3

.... and keep the loop when Till there are no rows left in the CSV files.

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

  & lt ;? 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); $ Line = maximum (calculation ($ csv1), calculation ($ csv2), counts ($ csv3)); $ Finalcsv = array (); ($ I = 0; $ i & lt; $ line; $ 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));    

No comments:

Post a Comment