PHP multidimensional array from text file -
looking little here. i'm trying parse space delimited text file, , create multidimensional array. data file apache2 log file, experience apply many text files.
what i'm trying do read record(a line), then: 1. extract source ip address. 2. check if ip address has been seen before. 3. extract destination port of packet. 4. sum how many times ip address has sent packet port.
so conceptually:
src-ip port sum
-----------------------------------
111.222.111.222 - 22 - 3 times
232.1.45.8 - 23 - 23 times
- 80 - 1 time
- 5353 - 2 times
217.163.132.190 - 23 - 12 times
i can open file , extract source ip, check insure it's unique, , put in array if is. can't figure out how add destination port, second field in array, let alone how add count of said dest ports third field in array. examples i've found on www.php.net overly simplistic, popullating multidimensional array hand. none seem show how create multidimensional array , explain how populate dimensions programmatically.
the following script: `
if (($handle = fopen("firewall", "r")) !== false) { while (($data = fgetcsv($handle, 1000, " ")) !== false) { if(!in_array($data[8], $src_ip)){ array_push($src_ip, $data[8]); } foreach($data $value){ if(strstr($value, $dest_port)){ // --> i'm stuck array_push($src_ip[][], $value); print($data[8]). " "; print($value)."\n"; } } $row++; } } fclose($handle); $unique_ip = count($src_ip); print("$row dropped packets $unique_ip unique ip addresses.\n"); ?>` this final line of output: 1154 dropped packets 302 unique ip addresses.
if chunk of data i'm working helpful, let me know. it's firewall log.
any @ appreciated. cheers, terry.
updated :
if (($handle = fopen("firewall", "r")) !== false) { while (($data = fgetcsv($handle, 1000, " ")) !== false) { if (!isset($src_ip[$data[8]])) { $src_ip[$data[8]] = array(); } foreach ($data $value) { if (strstr($value, $dest_port)) { if (!isset($src_ip[$data[8]][$value])) $src_ip[$data[8]][$value] = array('count' => 0); $src_ip[$data[8]][$value]['count'] += (int)$this_rows_count; print($data[8]) . " "; print($value) . "\n"; } } $row++; } }
Comments
Post a Comment