hadenp Posted July 22, 2010 Share Posted July 22, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/208506-sorting-a-recursiveiteratoriterator-object/ Share on other sites More sharing options...
Mchl Posted July 22, 2010 Share Posted July 22, 2010 Sorting could be performed by dumping contents into a SplMaxHeap The problem here is that you'd need to sort recursively each 'sub-iterator'. Quote Link to comment https://forums.phpfreaks.com/topic/208506-sorting-a-recursiveiteratoriterator-object/#findComment-1089450 Share on other sites More sharing options...
Daniel0 Posted July 22, 2010 Share Posted July 22, 2010 Of course you cannot sort an iterator. Iterators just iterate over some sort of object. If you wish to change the order then you have to sort the elements you're iterating over. Quote Link to comment https://forums.phpfreaks.com/topic/208506-sorting-a-recursiveiteratoriterator-object/#findComment-1089783 Share on other sites More sharing options...
hadenp Posted July 23, 2010 Author Share Posted July 23, 2010 >>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. Quote Link to comment https://forums.phpfreaks.com/topic/208506-sorting-a-recursiveiteratoriterator-object/#findComment-1090157 Share on other sites More sharing options...
Mchl Posted July 23, 2010 Share Posted July 23, 2010 Given that it's a recursive data structure... no As I said, you can use SplMaxHeap. You can use ArrayIterator to iterate over RecursiveDirectoryIterator and its children. Quote Link to comment https://forums.phpfreaks.com/topic/208506-sorting-a-recursiveiteratoriterator-object/#findComment-1090197 Share on other sites More sharing options...
hadenp Posted July 23, 2010 Author Share Posted July 23, 2010 I believe SPLMaxHeap starts with 5.3. but I'm working in 5.2.x environments. Would you have a suggestion on how to sort these iterators in 5.2? - I'm fairly new to PHP. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/208506-sorting-a-recursiveiteratoriterator-object/#findComment-1090290 Share on other sites More sharing options...
gizmola Posted July 23, 2010 Share Posted July 23, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/208506-sorting-a-recursiveiteratoriterator-object/#findComment-1090388 Share on other sites More sharing options...
hadenp Posted July 25, 2010 Author Share Posted July 25, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/208506-sorting-a-recursiveiteratoriterator-object/#findComment-1090976 Share on other sites More sharing options...
Mchl Posted July 25, 2010 Share Posted July 25, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/208506-sorting-a-recursiveiteratoriterator-object/#findComment-1091024 Share on other sites More sharing options...
gizmola Posted July 26, 2010 Share Posted July 26, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/208506-sorting-a-recursiveiteratoriterator-object/#findComment-1091376 Share on other sites More sharing options...
Daniel0 Posted July 26, 2010 Share Posted July 26, 2010 you can make any class foreach-able by implementing the traversable interface Traversable is a built-in internals-only interface. User land code will have to use the Iterator interface. Quote Link to comment https://forums.phpfreaks.com/topic/208506-sorting-a-recursiveiteratoriterator-object/#findComment-1091443 Share on other sites More sharing options...
Mchl Posted July 27, 2010 Share Posted July 27, 2010 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. Does she have LMGTFY letters on her head now? Quote Link to comment https://forums.phpfreaks.com/topic/208506-sorting-a-recursiveiteratoriterator-object/#findComment-1091860 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.