Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Multi-dimensional arrays

Posted by aparks (aparks), 9 February 2004
I have some firewall log data file of the form:

Source IP, Destination IP, Protocol, Destination Port,,UDP,520,,UDP,520,,UDP,520,,UDP,137,,UDP,137,,UDP,138,,TCP,23

and I'd like to present this on a web page so that it ends up like:

Source IP, Destination IP, Protocol, Destination Port, Number of packets,,UDP,520,3,,UDP,137,2,,UDP,138,1,,TCP,23,1

But I'm not sure the best way to go about this. I'm thinking that it can be done by reading each field in with a multi-dimensional array with code something like:

list($sourceip,$destip,$protocol,$port)= explode(",",$logfile_line);

and then run through this in a nested loop to print it all out in a table, but is this an efficient way to go about it? Is there a better way? Multi-dimensional arrays make my head hurt!


Adrian (looking for the aspirin)

Posted by admin (Graham Ellis), 9 February 2004
I would be inclined just to use the whole of each logfile line as one massive key and write

and the output using
foreach (array_keys ($packet) as $current) {
    print "$current,$packet[$current]\n";

Oh - you'll need to trim the new line off the log file line - either when you read it in or before you output it.

Where a multidimensional array would start to be useful is if you wanted to start providing totals of rows / columns etc.

Posted by aparks (aparks), 9 February 2004
That looks much better - thanks!

Posted by aparks (aparks), 9 February 2004
er...slight problem still! The code produces the output I was looking for, but also a lot of warnings of the following type:

Notice: Undefined index,192,168.1.1,TCP,21 in script.php on line xxx

referring to the line


This is presumably because this is the first time $packet[$logfile_line] is referred to, as it hasn't been previously defined. But how do I suppress these messages? I assume I could turn them off globally somewhere in php.ini on the server, but is it possible from within the code?



Posted by admin (Graham Ellis), 9 February 2004
Two possible solutions ...

You could check whether the array member exists (see isset) ... inialise it to 1 if it is new and increment it by one if it already exists

or ...

You turn off notices by specifiying
error_reporting (E_ALL & ~ E_NOTICE);

before you start building us the array

Posted by aparks (aparks), 9 February 2004
That's done it! Many thanks for all your help, Graham!


This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho