Jump to content

Recommended Posts

In trying to list directories using RecursiveIteratorIterator, I get unsorted results under Linux - (OS X presents the results in ascending order).

 

$startPath = 'documents/';

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($startPath),

            RecursiveIteratorIterator::SELF_FIRST);

 

sort($iterator); // Under Linux I get a warning that sort expects an array

 

Any idea how can I perform a sort a RecursiveIteratorIterator object?

TIA

>>If you wish to change the order then you have to sort the elements you're iterating over.

 

That makes sense, but would you have some suggestions on how to do this? Given that I have an iterator object, do I need to cast it to an array and then sort the array(s).

 

Moreover, are there simpler (non-recursive) ways to achieve a sorted list of directories/sub-directories?

Thanks.

 

why not just iterate through and dump the pathnames to an array?

 

$startPath = 'documents/';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($startPath),
            RecursiveIteratorIterator::SELF_FIRST);

$paths = array();

foreach ($iterator as $dirIt => $fileObj) {
  $paths[] = $fileObj->getPathname();
}
sort($paths); 

 

 

Thank you! Wish I'd checked back earlier. In the interim I used exec() w/ ls to get the results I needed - a bit of a hack but it works.

 

Question about:

foreach ($iterator as $dirIt => $fileObj)  

 

I didn't know how to reference the returned iterator like this. How/where does one find out usage examples? Googling? I didn't see such examples  when I looked up RecursiveDirectoryIterator, RecursiveIteratorIterator in the PHP:SPL - Manual.

 

Thanks again. BTW, Great avatar!

This information is in manual although it's a bit implicit. You'll notice that manual says RecursiveIteratorIterator implements Traversable interface, which is one of 'special' interafaces in PHP, which tells both interpreter and user that class implementing it can be traversed with foreach.

Thank you! Wish I'd checked back earlier. In the interim I used exec() w/ ls to get the results I needed - a bit of a hack but it works.

 

Question about:

foreach ($iterator as $dirIt => $fileObj)  

 

I didn't know how to reference the returned iterator like this. How/where does one find out usage examples? Googling? I didn't see such examples  when I looked up RecursiveDirectoryIterator, RecursiveIteratorIterator in the PHP:SPL - Manual.

 

Thanks again. BTW, Great avatar!

 

Glad to help.  I assumed that since you were using an interator you knew about foreach().  Pretty much the goal of using iterators in PHP is so you can use foreach, and as Mchl pointed out, you can make any class foreach-able by implementing the traversable interface, and with php5 they put in a lot of work to make all sorts of builtin classes traversable.  By default all arrays are traversable.

 

My Avatar is a funny picture of my daughter when she was still a baby.  She's 7 now -- as they say, it's very true that they grow up fast.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.