2017-04-24 19 views
1

我通过使用php curl接收数据并将它们保存到文本文件中。 它会自动刷新并在旧文本上写入数据。如何在旧文本文件中整理新行并覆盖到其他文本文件

所以,我试着编写一个函数,它将在旧的*文本文件中整理新的*行,并保存新的*文本文件。但我分开了。我能为它做些什么?

旧文本文件

line6) 6666666666 
line5) 5555566666 
line4) 4444444444 
line3) 3333333333 
line2) 2222222222 
line1) 1111111111 

新卷曲数据

line9) 9999999999 
line8) 8888888888 
line7) 7777777777 
line6) 6666666666 
line5) 5555566666 
line4) 4444444444 
line3) 3333333333 
line2) 2222222222 
line1) 1111111111 

应writen新线到新的文本文件

line9) 9999999999 
line8) 8888888888 
line7) 7777777777 

,我不想使用dublicate数据。 这里是我已经使用的一个例子。 (请仔细阅读所有行号)

line9) 9999999999 
line8) 8888888888 
line7) 7777777777 
line6) 6666666666 
line5) 5555566666 
line4) 4444444444 
line3) 3333333333 
line2) 2222222222 
line1) 1111111111 
line6) 6666666666 
line5) 5555566666 
line4) 4444444444 
line3) 3333333333 
line2) 2222222222 
line1) 1111111111 

回答

1

不知道你是否仍在寻找答案,但这就是我的意见。如果我正确理解你需要什么,它会完成这项工作......

<?php 

error_reporting(E_ALL); ini_set('display_errors', 1); 

$newcontent1 = array(); // set new array for future content 
$newcontent2 = array(); // set new array for future content 

$array1 = array('9999999999', '8888888888', '0000000000', '6666666666', '5555555555', '4444444444', '3333333333', '2222222222', '1111111111'); 
$array = array(0 => '9999999999', 1 => '8888888888', 2 => '0000000000', 3 => '6666666666', 4 => '5555555555', 5 => '4444444444', 6 => '3333333333', 7 => '2222222222', 8 => '1111111111'); 
// 2 different output, but it doesn't really matter, just for testing... 

echo"Array is :<br />"; 
print_r($array); 
echo"<br /><br />"; 

echo"File content :<br />"; 

$file = "test.inc.txt"; // a regulat txt file similar to your 'old text file' 
$filec = file_get_contents($file); // get file in a var 

print_r($filec); // only for checking purpose -> remove it when prod mode 

echo"<br /><br />"; 

foreach ($array as $value) 
    { // parse array to get the value of each entry 
    if(!in_array($value, $newcontent1)) { array_push($newcontent1, "$value\n"); // we push value to temp array #1 
    echo"(value of array entry -> $value)<br />"; // only for checking purpose -> remove it when prod mode 
    } else { /* do nothing */ } 
    } 

echo"<br /><br />"; 
echo"New temp content 1:<br />"; 
print_r($newcontent1); // only for checking purpose -> remove it when prod mode 
echo"<br /><br />"; 

$fp = fopen("test.inc.txt","r"); // we open file 

    while($line = fgets($fp)) { // parse file data to get each line 
     $line = trim($line); 
     if(!in_array($line, $array1)) { array_push($newcontent2, "$line\n"); echo"(value of file line -> $line)<br />"; } else { /* do nothing */ } 
    } 

echo"<br /><br />"; 
echo"New temp content 2:<br />"; 
print_r($newcontent2); // only for checking purpose -> remove it when prod mode 
echo"<br /><br />"; 

echo"New content (sorted ASC):<br />"; 

// *important part below* -> we combine the 2 temp arrays into one with unique value 
$newcontent = array_unique(array_merge($newcontent1, $newcontent2)); 

sort($newcontent); // rsort($newcontent); for desc order 

file_put_contents('output.txt', $newcontent); // we write new content of array to file 
// here, you may want to had checking upon creation of new file 

print_r($newcontent); 

echo"<br /><br />done !"; 

?> 
+0

谢谢OldPadawan。你对这个问题有什么看法? > http://stackoverflow.com/questions/43620072/removing-duplicate-lines-from-multiple-2-text-files-in-php – Andrew