<?php教程
//定義一個數組,用於存放排重後的結果
$result = array();
//讀取uid列表文件
$fp = fopen('test.txt', 'r');
while(!feof($fp))
{
$uid = fgets($fp);
$uid = trim($uid);
$uid = trim($uid, "r");
$uid = trim($uid, "n");
if($uid == '')
{
continue;
}
//以uid為key去看該值是否存在
if(empty($result[$uid]))
{
$result[$uid] = 1;
}
}
fclose($fp);
//將結果保存到文件
$content = '';
foreach($result as $k => $v)
{
$content .= $k."n";
}
$fp = fopen('result.txt', 'w');
fwrite($fp, $content);
fclose($fp);
?>
<?php
//定義數組,用於存放排重後的結果
$result = array();
//讀取第一個uid列表文件,放入$result_1
$fp = fopen('test_1.txt', 'r');
while(!feof($fp))
{
$uid = fgets($fp);
$uid = trim($uid);
$uid = trim($uid, "r");
$uid = trim($uid, "n");
if($uid == '')
{
continue;
}
//以uid為key寫入$result,如有重復就會覆蓋
$result[$uid] = 1;
}
fclose($fp);
//讀取第二個uid列表文件,並進行排重操作
$fp = fopen('test_2.txt', 'r');
while(!feof($fp))
{
$uid = fgets($fp);
$uid = trim($uid);
$uid = trim($uid, "r");
$uid = trim($uid, "n");
if($uid == '')
{
continue;
}
//以uid為key去看該值是否存在
if(empty($result[$uid]))
{
$result[$uid] = 1;
}
}
fclose($fp);
//$result裡保存的就排重以後的結果,可以輸出到文件,代碼省略
?>