How to display duplicate data in php text data? How can I delete duplicate data and keep only one of them?

Source: Internet
Author: User
How to display duplicate data in php text data? How can I delete duplicate data and keep only one of them? Data File ip.txt, some of which have the same IP data, such as: 1348453617 | 95.6.5.162 | 2012-09-2410: 26: 571348453024 | 180.254.252.144 | 2012-09-2410: 17: 04134 php text data, how to display duplicate data? How can I delete duplicate data and keep only one of them?
Data File ip.txt, some of which have the same IP data, such:

1348453617 | 95.6.5.162 | 10:26:57
1348453024 | 180.254.252.144 | 10:17:04
1348453020 | 180.254.252.144 | 10:17:00
1348453018 | 180.254.252.144 | 10:16:58
1348453008 | 112.210.114.169 | 10:16:48
1348453007 | 112.210.114.169 | 10:16:47
1348372772 | 119.178.17.181 | 11:59:32
1348363141 | 110.187.62.65 | 09:19:01

How to write a php file that only displays the content with duplicate IP data, such:
1348453024 | 180.254.252.144 | 10:17:04
1348453020 | 180.254.252.144 | 10:17:00
1348453018 | 180.254.252.144 | 10:16:58
1348453008 | 112.210.114.169 | 10:16:48
1348453007 | 112.210.114.169 | 10:16:47

Execute php and delete the duplicate data. only the last record of the duplicate data is retained:
1348453617 | 95.6.5.162 | 10:26:57
1348453024 | 180.254.252.144 | 10:17:04
1348453008 | 112.210.114.169 | 10:16:48
1348372772 | 119.178.17.181 | 11:59:32
1348363141 | 110.187.62.65 | 09:19:01


// Please help me write the php file to get the above results
$ Ipfile = "ip.txt ";
If ($ del = 1 ){
$ Old = file ("$ ipfile ");
$ Num = count ($ old );
$ Fp = fopen ("$ ipfile", "w ");
$ No = $ num-1;

For ($ I = 0; $ I <$ num; $ I ++ ){
List ($ usre, $ ip, $ time) = explode ("|", $ old [$ I]);
If ($ ip! = $ Delip) {fputs ($ fp, $ old [$ I]);}
}
Fclose ($ fp );
}
// Ip. php? Del = 1 & delip = $ in this way, all duplicate ip addresses will be deleted. how can this problem be solved to delete the duplicate IP address and retain the last duplicate IP address?
?>

------ Solution --------------------
For ($ I = 0; $ I <$ num; $ I ++ ){
List ($ usre, $ ip, $ time) = explode ("|", $ old [$ I]);
If ($ ip! = $ Delip) {fputs ($ fp, $ old [$ I]);}
}
Change
$ T = array ();
For ($ I = 0; $ I <$ num; $ I ++ ){
List ($ usre, $ ip, $ time) = explode ("|", $ old [$ I]);
If (isset ($ t [$ ip]) {
Unset ($ old [$ t [$ ip]);
Unset ($ t [$ ip]);
}
$ T [$ ip] = $ I;
}
Fputs ($ fp, join ('', $ old ));
------ Solution --------------------
$ Fp = fopen ("ip.txt", "r ");
While ($ file = fgets ($ fp )){

$ Arr [] = trim ($ file );
}
For ($ I = 0; $ I $ T = explode ("|", $ arr [$ I]);

$ T1 [] = array ("list" => $ t [0], "ip" => $ t [1], "time" => $ t [1]);

}
For ($ j = 0; $ j If ($ t1 [$ j] ['IP'] = $ t1 [$ j + 1] ['IP']) {
} Else {
$ T2 [] = $ t1 [$ j];
}
}
Fclose ($ fp );
$ Fp2 = fopen ("ip.txt", "w ");
Foreach ($ t2 as $ val ){
$ T3 = $ val ['list']. "|". $ val ['IP']. "|". $ val ['Time'];
Fwrite ($ fp2, $ t3. "\ r \ n ");
}
Fclose ($ fp2 );



?>
TESTED. No problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.