...

View Full Version : Multi-Dimensional Array Sort Possible?



gorilla1
01-17-2004, 09:38 PM
I have read in a file consisting of timestamp, pagevisitied and ip address, and I would like to display the contents ordered by ip address and ordered chronoligcally udner each ip address so that:

01.17.04 15:18:35 homepage xx.32.210.133
01.17.04 15:18:55 2ndpage xx.32.210.133
01.17.04 17:22:34 homepage xx.32.210.666
01.17.04 17:22:36 homepage xx.32.210.444
01.17.04 17:22:38 3rdpage xx.32.210.666

sorts to:
01.17.04 15:18:35 homepage xx.32.210.133
01.17.04 15:18:55 2ndpage xx.32.210.133
01.17.04 17:22:34 homepage xx.32.210.666
01.17.04 17:22:38 3rdpage xx.32.210.666
01.17.04 17:22:36 homepage xx.32.210.444

etc.
Below is how I read the file and create the array of each line...

$data = file("stuff.txt');
//
$data = array_reverse($data);
foreach($data as $key=>$element) {

$pieces = explode("|", $element);

I can't figure out how to set up a multidimensional arry with this and then sort on the two columsn, ip address and timestamp. Is there any way to do this without putting it in a database?

G

firepages
01-18-2004, 05:58 AM
Hi , I am sure there is a way , but pretty sure it would be messy ;)


to use arrays to achieve the same effect as SQL you would need to build several indexes (eg more arrays) based on your original array and then sort them somehow.
I did have a quick play with array_multisort() & friends but it got messy quite quickly.. also I am assuming the logfiles would be much larger than your example !

If you do have a database available I would suggest using it , you don't even have to log to the DB if you do not want to , you could easly just load the data into the db when you need to parse it.... a simplistic example ,



<?
//db.logparse = (varchar)[date,time,page,ip]
foreach( file('logfile.txt') as $yak){
$bits = explode( ' ' , $yak ) ;
mysql_query("INSERT INTO logparse VALUES('". implode('\',\'',$bits) ."')")or die("".__LINE__.mysql_error());
}
$yak = mysql_query("SELECT * FROM logparse ORDER BY ip,date,time");
while( $r = mysql_fetch_row( $yak ) ){
echo implode( ' ' , $r ).'<br />';
}
mysql_query("TRUNCATE TABLE logparse ") ;
?>


+ if you concatenated the date and time you could use a MySQL timestamp for storage of the first 2 fields.

gorilla1
01-19-2004, 01:26 AM
Firepages,
Thanks for saving me the time. I took your word for it, and laoded to db and then truncated table. Works nicely.

G



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum