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: 041348453020 | 180.254.252.144 | 2. how does one 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:
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