【问题标题】:Removing duplicate lines from multiple (2) text files in PHP从 PHP 中的多个 (2) 文本文件中删除重复行
【发布时间】:2017-04-25 20:09:11
【问题描述】:

我有 2 个 .txt 文件。 第一个 .txt 文件是 curl 数据(机器人),它总是得到 2000 行 .txt 行,包括新行

第二个 .txt 文件有第一个 .txt 文件的新数据。 我使用第二个 .txt 文件作为脚本。

我无法删除重复项。 (我的意思是我尝试根据旧值获取新值)所以脚本总是使用新旧数据。

有没有办法打开所有文件,删除重复项并将行相应地保存到第二个文件?

有三个刷新示例

这是第一次刷新和 2 个 .txt 文件

第一个 .txt 文件(你应该认为它有 2000 行)刷新 curl 机器人

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

我将使用的第二个 .txt 文件

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

这是第二次刷新和 2 个 .txt 文件

第一个 .txt 文件(你应该认为它有 2000 行)刷新 curl bot

Something here14
Something here13
Something here12
Something here11
Something here10
Something here9
Something here8
Something here7
Something here6
Something here5

我将使用的第二个 .txt 文件

Something here14
Something here13
Something here12
Something here11

这是第三次刷新和 2 个 .txt 文件

第一个 .txt 文件(你应该认为它有 2000 行)刷新 curl bot

Something here16
Something here15
Something here14
Something here13
Something here12
Something here11
Something here10
Something here9
Something here8
Something here7

我将使用的第二个 .txt 文件

Something here16
Something here15

编辑: 我发布了两个新的刷新

这是第四次刷新和 2 个 .txt 文件

第一个 .txt 文件(你应该认为它有 2000 行)刷新 curl bot

Something here20
Something here19
Something here18
Something here17
Something here16
Something here15
Something here14
Something here13
Something here12
Something here11

我将使用的第二个 .txt 文件

Something here20
Something here19
Something here18
Something here17

这是第五次刷新和 2 个 .txt 文件

第一个 .txt 文件(你应该认为它有 2000 行)刷新 curl bot

Something here24
Something here23
Something here22
Something here21
Something here20
Something here19
Something here18
Something here17
Something here16
Something here15

我将使用的第二个 .txt 文件

Something here24
Something here23
Something here22
Something here21

【问题讨论】:

  • @rizier123 Rizier123 在吗?
  • 对我来说不是很清楚...您需要将所有文件合并为一个没有重复的新文件吗?还是每次都合并first.txt + second.txt = new-with-no-duplicate.txt
  • @oldpadawan 不,只有 first.txt 和 second.txt。我只使用第二个文本。第一个是机器人。我摸不着。输出是第二个文本
  • 还是不明白...将第一次刷新+第二次刷新合并到一个没有重复的新文件中?你能举一个简单的例子来说明最后的预期吗?即:第一个数据/第二个数据=最终数据
  • @mackprogramsalot 你能给我你的电子邮件吗

标签: php python file curl


【解决方案1】:

(读取和解释 cmets)我认为您需要以下代码,使用 PHP array push

<?php

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

$array1 = array('here9', 'here8', 'here7', 'here6', 'here5', 'here4', 'here3', 'here2', 'here1');
$array2 = array('here4', 'here3', 'here2', 'here1');

echo"Array 1:<br />"; // just checking -> will be removed
print_r($array1); // just checking -> will be removed

echo"<br /><br />Array 2:<br />"; // just checking -> will be removed
print_r($array2); // just checking -> will be removed

echo"<br /><br />"; // will be removed

$newarray = array(); // create new empty array to receive new data

foreach ($array1 as $value) { /* parse array */

// here, we'll make use of PHP array_push
if( !in_array($value, $array2) ) { // if value is not in 2nd array

array_push($newarray, $value); // we add to new array we created

} else { /* do nothing */ }
    }

echo"New array with duplicate removed:<br />"; // just checking -> will be removed
print_r($newarray); // just checking -> will be removed

file_put_contents('output.txt', $newarray); // we write new content of array to file

?>

【讨论】:

    【解决方案2】:

    我试图将其保持在尽可能高的水平,但本质上是将每一行推入一个数组,然后使用 array_unique 删除重复项:

        $line_array = array();
        $files = getFiles();
        foreach($files as $file)
        {
            $lines = $file->getAllLines();
            foreach($lines as $line)
            {
                $line_array[] = $line;
            }
        }
        $without_duplicates = array_unique($line_array);
    

    【讨论】:

    • @mackprogramsalot $file1 = @file("first.txt");和 $file2 = @file("second.txt"); ???
    • getAllLines() 返回文件的所有行 getFiles() 获取文件也许这种类型的伪代码在堆栈溢出时不受欢迎?
    • @mackprogramsalot 我如何在此处添加 .txt?文件获取内容?
    猜你喜欢
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 2010-11-17
    • 2018-01-31
    • 2010-09-15
    • 1970-01-01
    • 2013-09-01
    相关资源
    最近更新 更多