Php 如何在旧文本文件中整理新行并覆盖到另一个文本文件

Php 如何在旧文本文件中整理新行并覆盖到另一个文本文件,php,Php,我使用php curl接收数据并将其保存到文本文件中。 它会自动刷新并在旧文本上写入数据 因此,我尝试编写一个函数,该函数将排序旧*文本文件中的新*行,并保存新*文本文件。但我崩溃了。我能为它做些什么 旧文本文件 新的旋度数据 应将新行写入新文本文件 我不想使用的重复数据。 这里有一个我已经使用过的例子。(请仔细阅读所有行号) 不知道你是否还在寻找答案,但这就是我的答案。如果我正确理解你需要什么,它会完成任务 <?php error_reporting(E_ALL); ini_set(

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

因此,我尝试编写一个函数,该函数将排序旧*文本文件中的新*行,并保存新*文本文件。但我崩溃了。我能为它做些什么

旧文本文件

新的旋度数据

应将新行写入新文本文件


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


不知道你是否还在寻找答案,但这就是我的答案。如果我正确理解你需要什么,它会完成任务

<?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 !";

?>

谢谢你,老学徒。你对这个问题有什么看法吗?>
line9) 9999999999
line8) 8888888888
line7) 7777777777
line6) 6666666666
line5) 5555566666
line4) 4444444444
line3) 3333333333
line2) 2222222222
line1) 1111111111
line9) 9999999999
line8) 8888888888
line7) 7777777777
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
<?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 !";

?>