Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP code to compare two large text files with ~300,000 entries and output differences

Tags:

function

php

i've got two lists A and B, B = A + C - D. All elements are unique, no duplicates. How do i get the lists of:
(1) the new items added, C
(2) the old items removed, D

C and D aren't more than 10000 elements or so.

Edit

Crap, sorry guys - forgot the important detail - these are both text files, not in memory elements.

like image 971
Dave Avatar asked Jan 24 '26 03:01

Dave


1 Answers

I think the size of arrays is irrelevant unless you really want to focus on how performant this operation is going to be i.e., you are going for a specific number of executions per unit of time.

If you just need to do it to get it done, it seems pretty trivial to me using array_diff()

$a = array( 1, 2, 3, 4 );
$b = array( 1, 3, 5, 7 ); // 2 and 4 removed, 5 and 7 added

$c = array_diff( $b, $a ); // [5, 7]
$d = array_diff( $a, $b ); // [2, 4]
like image 170
Peter Bailey Avatar answered Jan 25 '26 17:01

Peter Bailey