Web Analytics Made Easy -
StatCounter Sorting arrays .... - CodingForum

Announcement

Collapse
No announcement yet.

Sorting arrays ....

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sorting arrays ....

    Alright, here's the code ...

    PHP Code:
    <?php

    print("<tr>\n");
    print(
    "<td><b>Game</b></td>\n");
    print(
    "<td><b>Filesize</b></td>\n");
    print(
    "<td><b>Description of SPCs</b></td>\n");
    print(
    "<td><b>Date</b></td>\n");
    print(
    "<td><b>Supplier</b></td>\n");
    print(
    "</tr>\n\n");

    $files_dir getcwd();
    $dir_handle opendir($files_dir);
    $files = array();

    while(
    $filename readdir($dir_handle))
    {
         
    array_push($files$filename);
    }

    closedir($dir_handle);

    sort($files);
    reset($files);

    foreach(
    $files as $filename)
    {
         if(
    $filename == "./" OR $filename == "..")
         {
              continue;
         }

         
    $ext = array("frm""FRM");
         
    $fileExt explode("."$filename);

         if(!
    in_array($fileExt[sizeof($fileExt) - 1], $ext))
         {
              continue;
         }

         
    $data file($filename);

         
    $archrar strtolower(trim($data[0]));
         
    $archsize filesize("$archrar.rar");

         if(
    $archsize <= 1048575)
         {
              
    $archsize round($archsize 1024) . " KB";
         }

         if(
    $archsize >= 1048576)
         {
              
    $archsize round(($archsize 1024 1024), 1) . " MB";
         }

         
    $archname trim($data[1]);
         
    $archinfo trim($data[2]);
         
    $archdate trim($data[3]);
         
    $archsupp trim($data[4]);

         print(
    "<tr>\n");
         print(
    "<td><a href=\"$archrar.rar\">$archname</a></td>\n");
         print(
    "<td>$archsize</td>\n");
         print(
    "<td>$archinfo</td>\n");
         print(
    "<td>$archdate</td>\n");
         print(
    "<td>$archsupp</td>\n");
         print(
    "</tr>\n\n");

         
    clearstatcache();
    }

    ?>
    What I need to do is sort the $files array by the string(s) specified later in $archname for each instance of $filename (so the tables can display in alphabetical order) and without actually changing the values in $files. I can't seem to figure this out. Could anyone help? I should note that some of the strings have multiple spaces, so this would need to be accounted for ..

    - Frank

  • #2
    Come on, anyone ..? ;_;

    Comment


    • #3
      gimme an example (both in code (of data values), and in real-world of expected results) so I can better understand your need.

      -Celt

      Comment


      • #4
        This is what it's doing:



        But I need it in alphabetical order, by the name in $archname and not by the actual filename.

        Comment


        • #5
          create an array outside of the loop.

          store each file's info into that array.

          then, sorting that array by $archname, loop through that array, printing out the rows. (you may find it easier to create an associative to handle the file info)

          Comment


          • #6
            I've more or less already tried this with no luck. Could you elaborate more? Just a little pseudo-code or something

            Comment


            • #7
              Take a look at the manual page for usort(). It's quite helpful for your task: http://www.php.net/usort

              Basically it works like the sort function, but you pass the name of a comparison function as a second parameter. In this user-defined comparison function you always get two arguments passed, and these refer to the fields of the array. In the body of the comparison function you can finetune the comparison to suit your needs.

              Example:

              PHP Code:
              $people = array(
                  array(
              'name' => 'Miller',         'age' => 27),
                  array(
              'name' => 'Smith',         'age' => 99),
                  array(
              'name' => 'Simpson',         'age' => 12),
                  array(
              'name' => 'Henderson',     'age' => 35)
              );

              function 
              compare($a$b) { 
                 return 
              strcmp($a['name'], $b['name']); 


              usort($people'compare');

              for (
              $i 0$i count($people); $i++) {
                  print 
              $people[$i]['name'] . "<br />";

              De gustibus non est disputandum.

              Comment

              Working...
              X