?
Solved

PHP readdir(): Sort results alphanumerically

Posted on 2010-09-14
10
Medium Priority
?
468 Views
Last Modified: 2012-05-10
I use the attached code to return a list of directories.  How can I display the results sorted alphanumerically?
if ($handle = opendir('directory')) {
    while (($file = readdir($handle)) !==false) {
        if (substr($file,0,1) != "." && is_dir($file)) {
	echo $file;
        }
    }
    closedir($handle);
}

Open in new window

0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 15

Accepted Solution

by:
ludofulop earned 1000 total points
ID: 33671180

if ($handle = opendir('directory')) {
$dirs = array()
    while (($file = readdir($handle)) !==false) {
        if (substr($file,0,1) != "." && is_dir($file)) {
	$dirs[] = $file;
        }
    }
    closedir($handle);
    sort($dirs);
    echo implode('<br />', $dirs);
}

Open in new window

0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 total points
ID: 33671183
Gather the data into an array and sort the array.
if ($handle = opendir('directory')) 
{
    while (($file = readdir($handle)) !==false) 
    {
        if (substr($file,0,1) != "." && is_dir($file)) 
        {
	    $arr[] = $file;
        }
    }
    closedir($handle);
}

sort($arr);
var_dump($arr);

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 33671188
@ludofulop: Proof that great minds think alike!  ;-)

~Ray
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 33671206
@hankknight: Here is a man page worth reading as you choose the exact kind of sort you need.

http://us.php.net/manual/en/array.sorting.php
0
 
LVL 13

Expert Comment

by:darren-w-
ID: 33672343
I think ksort may be better?
<?php
$directory = new directoryListing("../testfolder");
print_r($directory->sortListing());

class directoryListing {
   private $_dPath;
   private $_listing = array();
   public function __construct($path) {
      $this->_dPath = $path;
   }
   private function getListing() {
      if ($handle = opendir($this->_dPath)) {
         while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != ".." && !is_dir($file)) {
               $this->_listing[] = $file;
            }
         }
         closedir($handle);
      }
   }
   public function sortListing()
   {
      $this->getListing();
      ksort($this->_listing);
      return $this->_listing;      
   }
}
?>

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 33672510
Please see:
http://us3.php.net/manual/en/function.ksort.php

There are only numbered keys in the arrays we built here, so the sort order with ksort will not change.
0
 
LVL 13

Expert Comment

by:darren-w-
ID: 33672786
Hi Ray,

Sorry, sort does work fine, it must of been the folder structure I was testing it on, one thing I did find was wrong in the previous examples was that  is_dir($file)  needs to be ! is_dir($file)  as it was not returning any listings.

Darren
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 33672817
I think the OP wanted to sort a list of directories.  That is probably why we found is_dir() in his original post.
0
 
LVL 13

Expert Comment

by:darren-w-
ID: 33672950
Yep, know that , that what the class returns...
0
 
LVL 16

Author Closing Comment

by:hankknight
ID: 33773736
Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question