1
<?php
$new = array("12", "11", "10", "9", "8", "7", "6", "5", "4");
$old = array("10", "9", "8", "7", "6", "5", "4", "3", "2", "1");

$result = array_diff($new, $old);

print_r($result);
?>

The result is Array ( [0] => 12 [1] => 11 ) so 12 and 11

According to this example how to use array_diff for (.txt) files in PHP?

Here are (.txt) examples:

New (.txt) file

Something here12
Something here11
Something here10
Something here9
Something here8
Something here7
Something here6
Something here5
Something here4
Something here3

Old (.txt) file

Something here10
Something here9
Something here8
Something here7
Something here6
Something here5
Something here4
Something here3
Something here2
Something here1

Output (.txt) file

Something here12
Something here11
Andrew
  • 243
  • 1
  • 10

1 Answers1

1

You just need to reproduce your arrays from the text files :

<?php
$new = explode(PHP_EOL, file_get_contents('new.txt').PHP_EOL);
$old = explode(PHP_EOL, file_get_contents('old.txt').PHP_EOL);
$result = array_diff($new, $old);

$output= fopen("Output.txt", "w") or die("Unable to open file!");
foreach($result as $value){
    fwrite($output, $value.PHP_EOL);
}

?>

I am not sure if you wanted the strip the "Something here" but in your exemple output.txt it seems like not.

Lou
  • 866
  • 5
  • 14
  • it gives different result :/ – Andrew Apr 28 '17 at 20:07
  • 1
    You don't need to add `PHP_EOL`, since `file()` includes the newlines unless you use the `FILE_IGNORE_NEW_LINES` option. – Barmar Apr 28 '17 at 20:08
  • @barmar But It doesnt the same output. The output should be 12, 11 – Andrew Apr 28 '17 at 20:18
  • @lou need to delete new line last of txt? PHP_EOL edd blank line at the end of the text. $fp = preg_replace('/^[ \t]*[\r\n]+/m', '', $fp); $fp = preg_replace('/[\r\n]+$/', '', $fp); these are doesnt work – Andrew May 15 '17 at 08:03
  • @barmar how to use FILE_IGNORE_NEW_LINES php_eol edd new line at the end of text – Andrew May 15 '17 at 08:05
  • 1
    @Andrew `$new = file_get_contents('new.txt', FILE_IGNORE_NEW_LINES);` – Barmar May 16 '17 at 15:38