如何使用 php 比较两个文件来找到多余的行?

发布于 2025-01-08 17:14:01 字数 395 浏览 4 评论 0 原文

我有两个文件。

第一个有 482 行。 第二个只有 519 行。

我想通过使用 php.ini 比较两个文件来找到额外的行。

假设我的第一个文件有这样的行

Mango
Orange
Cherry
Apple 
Blackberry

,假设我的第二个文件看起来像这样

Apple 
Orange
Mango
Banana
Cherry
Blackberry

请注意:这些行是随机顺序的。现在我想使用 php 脚本删除相同的行并保留多余的行。例如,文件 1 包含行 Mango。文件 2 也包含该行,但顺序是随机的。所以我想删除该行。

I have two files.

First one has 482 lines.
Second one has only 519 lines.

I would like to find extra lines by comparing two files using php.

Lets say my first file has lines like this

Mango
Orange
Cherry
Apple 
Blackberry

And lets say my second file looks like this

Apple 
Orange
Mango
Banana
Cherry
Blackberry

Please note: The lines are in random order. Now i would like to use a php script to remove the same lines and keep the extra lines. For example File 1 contains line Mango. File 2 also contains that line but in random order. So i want to remove that line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

巨坚强 2025-01-15 17:14:01

将每个文件加载到字符串数组中(例如,使用 file_get_contents)。

执行一些循环,对于数组 2 中的每个项目,确定该项目是否存在于数组 1 中。如果存在,则从数组 2 中删除该项目并继续。

完成后,数组 2 将仅包含唯一的行。

编辑

如果您只想删除 File2 中同时存在于 File1 中的行,则您需要查找唯一值(其中顺序并不重要)。一种快速的方法是使用 array_diff 函数。

这是一个示例:

$file1 = array('Mango', 'Orange', 'Cherry', 'Apple', 'Blackberry');
$file2 = array('Apple', 'Orange', 'Mango', 'Banana', 'Cherry', 'Blackberry');

$diff = array_diff($file2, $file1);

var_dump($diff);

// Output
array
    3 => string 'Banana' (length=6)

如果您喜欢使用我在第一部分中提到的循环手动执行此操作,则可以按以下方式执行此操作:

// Loop over every value in file2
for($i = count($file2) - 1; $i >= 0; $i--)
{
    // Compare to every value in file1; if same, unset (remove) it
    foreach($file1 as $v)
        if ($v == $file2[$i])
        {
            unset($file2[$i]);
            break;
        }
}
// Reindex the array to remove gaps
$output = array_values($file2);
var_dump($output);

// Output
array
    0 => string 'Banana' (length=6)

Load each file into a string array (using file_get_contents, for example).

Perform some loops that, for every item in array 2, determine if the item exists in array 1. If so, remove the item from array 2 and continue.

When complete, array 2 will contain only unique lines.

Edit:

If you just want to remove lines in File2 that are also present in File1, you're looking for the unique values (where order does not matter). A quick way to do this is using the array_diff function.

Here is an example:

$file1 = array('Mango', 'Orange', 'Cherry', 'Apple', 'Blackberry');
$file2 = array('Apple', 'Orange', 'Mango', 'Banana', 'Cherry', 'Blackberry');

$diff = array_diff($file2, $file1);

var_dump($diff);

// Output
array
    3 => string 'Banana' (length=6)

If you prefer to do this manually using loops like I mentioned in the first part, here is how you would do it:

// Loop over every value in file2
for($i = count($file2) - 1; $i >= 0; $i--)
{
    // Compare to every value in file1; if same, unset (remove) it
    foreach($file1 as $v)
        if ($v == $file2[$i])
        {
            unset($file2[$i]);
            break;
        }
}
// Reindex the array to remove gaps
$output = array_values($file2);
var_dump($output);

// Output
array
    0 => string 'Banana' (length=6)
傲娇萝莉攻 2025-01-15 17:14:01

我采取了 JYelton 建议的相同方法。

此处演示:http://codepad.org/lCa68G76

<?

$file1 = array(
    'Mango',
    'Orange',
    'Cherry',
    'Apple',
    'Blackberry'
);

$file2 = array(
    'Apple',
    'Orange',
    'Mango',
    'Banana',
    'Cherry',
    'Blackberry'
);


foreach($file2 as $line)
{
    if (!in_array($line, $file1))
    {
        $output[] = $line;
    }
}

var_dump($output);


?>

I took the same approach JYelton has suggested.

Demo here: http://codepad.org/lCa68G76

<?

$file1 = array(
    'Mango',
    'Orange',
    'Cherry',
    'Apple',
    'Blackberry'
);

$file2 = array(
    'Apple',
    'Orange',
    'Mango',
    'Banana',
    'Cherry',
    'Blackberry'
);


foreach($file2 as $line)
{
    if (!in_array($line, $file1))
    {
        $output[] = $line;
    }
}

var_dump($output);


?>
檐上三寸雪 2025-01-15 17:14:01

通过将每个文件的行读入一个列表来创建两个列表,然后比较它们。遍历 list1 并删除 list2 中未找到的所有项目,反之亦然。

Make two lists by reading in the lines of each file into one list, and then compare them. Go through list1 and remove every item that isn't found in list2, or vice versa.

ι不睡觉的鱼゛ 2025-01-15 17:14:01
<?php

$testOne = 'Apple Orange Carrot Banana';
$testTwo = 'Apple Orange Carrot';

$tTwo = explode(' ', $testTwo);
$tOne = explode(' ', $testOne);

foreach($tOne as $first) {
    foreach($tTwo as $second) {
        if ($second == $first) {
            echo 'Both arrays contain: ' . $second . '</br>';
        }       
    }
}

?>

检查两个数组是否包含值。

<?php

$testOne = 'Apple Orange Carrot Banana';
$testTwo = 'Apple Orange Carrot';

$tTwo = explode(' ', $testTwo);
$tOne = explode(' ', $testOne);

foreach($tOne as $first) {
    foreach($tTwo as $second) {
        if ($second == $first) {
            echo 'Both arrays contain: ' . $second . '</br>';
        }       
    }
}

?>

Check if both arrays contain values.

肩上的翅膀 2025-01-15 17:14:01

这需要用 PHP 脚本来完成吗?您可以在 bash 中轻松实现此目的:

cat file1 file2 | sort | uniq > uniques.txt

Does this need to be done with a PHP script? You can achieve this in bash quite easily:

cat file1 file2 | sort | uniq > uniques.txt
喜你已久 2025-01-15 17:14:01
// read in both files
$file1 = file($filename1);
$file2 = file($filename2);

// calculate the entries that are in both files
$inBothFiles = array_intersect($file1, $file2);

// filter elements found in both files from file2 
$newFile2 = array_diff($file2, $inBothFiles);
// read in both files
$file1 = file($filename1);
$file2 = file($filename2);

// calculate the entries that are in both files
$inBothFiles = array_intersect($file1, $file2);

// filter elements found in both files from file2 
$newFile2 = array_diff($file2, $inBothFiles);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文