Take the first 1000 values of an array & lt ;? Php $ fnvarloghaproxy. log.2; $ fpfopen ($ fn, r) ordie (& quot; fileopen $ fnfalse & quot;); while ($ sfgets ($ fp) {& nbsp; preg_match ([[d. :] +]. + [([obtain the first 1000 values of an array
$ Fn = '/var/log/haproxy. log.2 ';
$ Fp = fopen ($ fn, 'r') or die ("file open $ fn false ");
While ($ s = fgets ($ fp )){
Preg_match ('/\ [\ d.:] + \]. + \ [([\ d.:] +) \]/', $ s, $ r );
If (empty ($ r [1]) continue;
@ $ Res [$ r [1] ++;
}
Fclose ($ fp );
Asort ($ res );
Print_r ($ res );
?>
------ Solution --------------------
You cannot solve this problem.
Separate processing .....
For example, you can write the ip address in the first place to 255 files, or convert the ip address to a number. in this way, you can use less memory for subsequent processing.
Find the first 255 files... Because each group contains at most 2 ^ 24 different numbers [ipv6 is not considered for the time being], this memory is acceptable
In this way, we can use an array to traverse the complexity of O (n) to find the first 1000.
Finally, maintain a 1000 ordered array and insert data into it. if the number exceeds 1000, the minimum