Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Multi-Dimensional Array Sort Possible?

    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

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,084
    Thanks
    11
    Thanked 100 Times in 98 Posts
    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 ,

    PHP Code:
    <?
    //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.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Firepages,
    Thanks for saving me the time. I took your word for it, and laoded to db and then truncated table. Works nicely.

    G


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •