Home > php教程 > php手册 > body text

php mysql百万级数据去除重复数据

WBOY
Release: 2016-06-13 10:07:31
Original
1804 people have browsed it


//定义一个数组,用于存放排重后的结果
$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);
?>  


//定义数组,用于存放排重后的结果
$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里保存的就排重以后的结果,可以输出到文件,代码省略
?>

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template