如何在旧文本文件中排序新行并覆盖到其他文本文件

时间:2017-04-24 11:15:58

标签: php

我通过使用php curl并将它们保存到文本文件来接收数据。 它会自动刷新并在旧文本上写入数据。

所以,我尝试编写一个函数来解析旧*文本文件中的新*行,并保存新的*文本文件。但我分开了。我能做些什么呢?

  

旧文本文件

var extendedPhrases = phrases
        .Select(x => new ExtendedPhrase()
        {
            Ajlpt = x.Ajlpt,
            Bjlpt = x.Bjlpt,
            Created = x.Created,
            CreatedLast = x.Created % 10
        });
  

新卷曲数据

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

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

line9) 9999999999
line8) 8888888888
line7) 7777777777

1 个答案:

答案 0 :(得分: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 !";

?>