Looking for a little help here. I'm trying to parse a space delimited text file, and create a multidimensional array. The data file is an Apache2 log file, but the experience would apply to many text files.
What I'm trying to do do is read a record(a line), then: 1. Extract the source ip address. 2. Check if this ip address has been seen before. 3. Extract the destination port of the packet. 4. Sum how many times this ip address has sent a packet to this 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 the file and extract the source ip, check to insure it's unique, and put it in an array if it is. What I can't figure out is how to add the destination port, as a second field in the array, let alone how to add a count of said dest ports as a third field in the array. All the examples I've found on www.php.net are overly simplistic, basically just popullating a multidimensional array by hand. None seem to show how to create a multidimensional array and then explain how to populate the dimensions programmatically.
The following is the 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 as $value){
if(strstr($value, $dest_port)){
// --> where 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 from $unique_ip unique ip addresses.\n");
?>`
This is the final line of the output: 1154 dropped packets from 302 unique ip addresses.
If a chunk of the data I'm working with would be helpful, let me know. It's just an firewall log.
Any help at all would be appreciated. Cheers, Terry.
$arr[$ip][$port][] = array(... other data fields here)
? – Marc B Mar 12 at 4:09