如何在PHP中使用array_diff(.txt)文件

时间:2017-04-28 19:36:00

标签: php array-difference

<?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);
?>
  

结果 Array ( [0] => 12 [1] => 11 ) 所以 12 and 11

     

根据这个例子,如何在PHP中使用array_diff(.txt)文件

     

以下是(.txt)示例:

新(.txt)文件

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

旧(.txt)文件

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

输出(.txt)文件

Something here12
Something here11

1 个答案:

答案 0 :(得分:1)

您只需要从文本文件中重现阵列:

<?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);
}

?>

我不确定你是否想要条带“Something here”但是在你的例子中输出.txt似乎没有。