Merge two large CSV files with PHP

I want to combine two large CSV files with PHP. These files are too large to be inserted into memory immediately. In pseudo code, I can come up with something like this:

for i in file1
  file3.write(file1.line(i) + ',' + file2.line(i))
end

But when I iterate over a file with fgetcsv, it is not entirely clear how I will take a line nfrom a specific file without first loading everything into memory.

Any ideas?

Edit: I forgot to mention that each of the two files has the same number of lines, and they are one-to-one. That is, line 62, 322 in file1 goes with line 62, 322 in file2.

+3
source share
5 answers

, , Linux, paste, , , PHP.

, PHP, :

paste -d ',' file1 file2 > combined_file
+4

fgets. , , , .

PHP: fgets

, # 1 PHP. fgets.

1 , , . , FALSE.

, FALSE, , .

+3

fgets().

$file1 = fopen('file1.txt', 'r');
$file2 = fopen('file2.txt', 'r');
$merged = fopen('merged.txt', 'w');

while (
    ($line1 = fgets($file1)) !== false
    && ($line2 = fgets($file2)) !== false) {

    fwrite($merged, $line1 . ',' . $line2);
}

fgets() . , , . :

http://php.net/fgets

http://php.net/fopen

http://php.net/fwrite

+3
+1

I think the solution for this is to match the first line for each line (and some kind of key if you need) and then create a new csv using fread and fwrite (we know the beginning and end of each line now, so we just need to search and read)

Another way is to put it in MySQL (if possible) and then revert to the new CSV

0
source

Source: https://habr.com/ru/post/1786509/


All Articles