Good time to all, please, tell me who is not lazy, for example ...

If BRIEF: there is an array of data:

01.09.2016|10:48:31|192.168.0.1|Cвязь с сервером прервана. 01.09.2016|10:48:32|192.168.0.1|Cвязь с сервером прервана. 01.09.2016|10:48:36|192.168.0.1|Cвязь с сервером прервана. 01.09.2016|16:41:34|192.168.0.2|Cвязь с сервером прервана. 01.09.2016|18:47:06|192.168.0.2|Cвязь с сервером прервана. 

Explod by array fields and we get the IP set below by the $fields[2] code, we get the set of IP addresses.

QUESTION: how to find out the number of all the same IP, how many 192.168.0.1, and how many 192.168.0.2 ???

As far as I can imagine, then: array_count_values() , how to implement?

Closed due to the fact that off-topic participants Dmitriy Simushev , rjhdby , aleksandr barakin , user207618, Streletz Oct 15 '16 at 11:31 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Dmitriy Simushev, rjhdby, aleksandr barakin, Community Spirit
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • You have already found the correct function, things are going to happen next. What specifically can not do? Where is your attempt code? - Dmitriy Simushev

2 answers 2

To do without explode, reading in rows, etc. enough to do this:

 $str = ...; $matches = null; $returnValue = preg_match_all('/.*\|.*\|(.*)\|.*/', $str, $matches); $ipCountMap = array_count_values($returnValue[1]); 
  • and what will be your performance efficiency of regular expressions on large files compared to post-reading? - teran
  • Naturally depends on file size. But on large files I would not solve this problem at all - the file still needs to be read. If we are talking about processing large volumes of constantly emerging logs, you can pick up some time-series database like unflux and store logs in it (if later on these logs any analytics is assumed). If the only thing needed is a task voiced by the vehicle - then I would use something like awk - Ilya Kovalyov
  • $str = "awk -F \"|\" '{print $2}' file.csv"; and further explode and counting - Ilya Kovalyov

Like so

 $ips = []; foreach ($rows as $fields) { $ips[] = $fields[2]; } $ipCountMap = array_count_values($ips); // echo '<pre>'; print_r($ipCountMap); die; // // Array // ( // [192.168.0.1] => 3 // [192.168.0.2] => 2 // ) 

UPDATE:

 <?php $arr = [ '01.09.2016|10:48:31|192.168.0.1|Cвязь с сервером прервана', '01.09.2016|10:48:32|192.168.0.1|Cвязь с сервером прервана', '01.09.2016|10:48:36|192.168.0.1|Cвязь с сервером прервана', '01.09.2016|16:41:34|192.168.0.2|Cвязь с сервером прервана', '01.09.2016|18:47:06|192.168.0.2|Cвязь с сервером прервана' ]; $ips = array_map(function($v) { $t = explode('|', $v); return $t[2]; }, $arr); $map = []; for($i=0; $i<count($arr); $i++) { if (!isset($map[$ips[$i]])) { $map[$ips[$i]] = []; } $map[$ips[$i]][] = $arr[$i]; } $totals = array_map('count', $map); print_r($map); print_r($totals); 

Result:

 Array ( [192.168.0.1] => Array ( [0] => 01.09.2016|10:48:31|192.168.0.1|Cвязь с сервером прервана [1] => 01.09.2016|10:48:32|192.168.0.1|Cвязь с сервером прервана [2] => 01.09.2016|10:48:36|192.168.0.1|Cвязь с сервером прервана ) [192.168.0.2] => Array ( [0] => 01.09.2016|16:41:34|192.168.0.2|Cвязь с сервером прервана [1] => 01.09.2016|18:47:06|192.168.0.2|Cвязь с сервером прервана ) ) Array ( [192.168.0.1] => 3 [192.168.0.2] => 2 ) 

http://sandbox.onlinephpfunctions.com/code/371cbd265de9487763d391d51631e1a7a79e88bd

  • Thank you very much, I have an example like this: - AlexPebody
  • $ arr = ['01 .09.2016 | 10: 48: 31 | 192.168.0.1 | The connection to the server is interrupted ', '01 .09.2016 | 10: 48: 32 | 192.168.0.1 | The connection to the server is interrupted', '01 .09.2016 | 10: 48: 36 | 192.168.0.1 | The connection with the server was interrupted ', '01. 09.2016 | 16: 41: 34 | 192.168.0.2 | The connection with the server was interrupted', '01 .09.2016 | 18: 47: 06 | 192.168 .0.2 | Communication with server terminated ']; // Searched IP $ srch = '192.168.0.1'; $ matches = array_filter ($ arr, function ($ item) use ($ srch) {return preg_match ('~'. preg_quote ($ srch). '~', $ item);}); - AlexPebody
  • if (! empty ($ matches)) {$ view = 'Total errors: <b>'. count ($ matches). '</ b> <hr />'. '<table border = "1" style = "background-color: # EEE5AE;">'. '<th> Date </ th> <th> Time </ th> <th> IP </ th> <th> Message </ th>'; foreach ($ matches as $ i => $ v) {list ($ date, $ time, $ ip, $ mesg) = explode ('|', $ v); $ view. = ($ i & 1? '<tr>': '<tr style = "background-color: # EEEEE0;">'). '<td>'. $ date. '</ td> <td>'. $ time. '</ td> <td>'. $ ip. '</ td> <td>'. $ mesg. '</ td> </ tr>'; } - AlexPebody
  • $ view. = '</ table>'; } else {$ view = 'Communication errors by IP: <b>'. $ srch. '</ b> not found! <br />'; } echo $ view; - AlexPebody
  • And how to display for each IP? Yes, and attached to the names, 192.168.0.1 => Office 1, 192.168.0.1 => Office 2, in order to finally display, Office 1 total errors - 25, Office 2 - total errors -10 - AlexPebody