2

I have two files.

First one has 482 lines. Second one has only 519 lines.

I would like to find extra lines by comparing two files using php.

Lets say my first file has lines like this

Mango
Orange
Cherry
Apple 
Blackberry

And lets say my second file looks like this

Apple 
Orange
Mango
Banana
Cherry
Blackberry

Please note: The lines are in random order. Now i would like to use a php script to remove the same lines and keep the extra lines. For example File 1 contains line Mango. File 2 also contains that line but in random order. So i want to remove that line.

Giri
  • 4,849
  • 11
  • 39
  • 48

7 Answers7

4

Load each file into a string array (using file_get_contents, for example).

Perform some loops that, for every item in array 2, determine if the item exists in array 1. If so, remove the item from array 2 and continue.

When complete, array 2 will contain only unique lines.

Edit:

If you just want to remove lines in File2 that are also present in File1, you're looking for the unique values (where order does not matter). A quick way to do this is using the array_diff function.

Here is an example:

$file1 = array('Mango', 'Orange', 'Cherry', 'Apple', 'Blackberry');
$file2 = array('Apple', 'Orange', 'Mango', 'Banana', 'Cherry', 'Blackberry');

$diff = array_diff($file2, $file1);

var_dump($diff);

// Output
array
    3 => string 'Banana' (length=6)

If you prefer to do this manually using loops like I mentioned in the first part, here is how you would do it:

// Loop over every value in file2
for($i = count($file2) - 1; $i >= 0; $i--)
{
    // Compare to every value in file1; if same, unset (remove) it
    foreach($file1 as $v)
        if ($v == $file2[$i])
        {
            unset($file2[$i]);
            break;
        }
}
// Reindex the array to remove gaps
$output = array_values($file2);
var_dump($output);

// Output
array
    0 => string 'Banana' (length=6)
JYelton
  • 35,664
  • 27
  • 132
  • 191
1

I took the same approach JYelton has suggested.

Demo here: http://codepad.org/lCa68G76

<?

$file1 = array(
    'Mango',
    'Orange',
    'Cherry',
    'Apple',
    'Blackberry'
);

$file2 = array(
    'Apple',
    'Orange',
    'Mango',
    'Banana',
    'Cherry',
    'Blackberry'
);


foreach($file2 as $line)
{
    if (!in_array($line, $file1))
    {
        $output[] = $line;
    }
}

var_dump($output);


?>
jon
  • 5,986
  • 5
  • 28
  • 35
0

Make two lists by reading in the lines of each file into one list, and then compare them. Go through list1 and remove every item that isn't found in list2, or vice versa.

Alexander Corwin
  • 1,097
  • 6
  • 11
  • Hello Alex, I'm a php beginner. Thats why i asked this question. So if you give me some sample code i would really appreciate it. Thanks – Giri Feb 23 '12 at 16:56
0
<?php

$testOne = 'Apple Orange Carrot Banana';
$testTwo = 'Apple Orange Carrot';

$tTwo = explode(' ', $testTwo);
$tOne = explode(' ', $testOne);

foreach($tOne as $first) {
    foreach($tTwo as $second) {
        if ($second == $first) {
            echo 'Both arrays contain: ' . $second . '</br>';
        }       
    }
}

?>

Check if both arrays contain values.

0

Does this need to be done with a PHP script? You can achieve this in bash quite easily:

cat file1 file2 | sort | uniq > uniques.txt
0
// read in both files
$file1 = file($filename1);
$file2 = file($filename2);

// calculate the entries that are in both files
$inBothFiles = array_intersect($file1, $file2);

// filter elements found in both files from file2 
$newFile2 = array_diff($file2, $inBothFiles);
Dan Soap
  • 10,114
  • 1
  • 40
  • 49