Re: something like grep



..oO(Jeff)

Michael Fesser wrote:

$path = '/foo/bar/whatever';
$di = new RecursiveDirectoryIterator($path);
foreach (new RecursiveIteratorIterator($di) as $item) {
if ($item->isFile()) {
...
}
}

Thanks,

I see that this gives an alphabetically sorted list.
/a.html
/a/a.html
/ab.html
/b.html

I find it fascinating that can be done, but not useful at the moment.

Can I look at the root first and then recurse through the directories?
Or is this easier with recursive readdir?

I think this requires some handcoding either way.

You would have to loop through a directory and for example read its
contents into two arrays, one for files, one for subdirectories. Then
sort these arrays if necessary. Process the file array first, then loop
through the second array to recursively process all the sub dirs. Even
here an SPL iterator could be helpful and convenient, because you can
store file objects in these arrays, which make it then very easy to get
their filenames, paths or whatever information you need.

Of course that's just a very rough idea. Maybe there's even a way to do
it with iterators alone, for example by deriving a child class from one
of them and adjusting some of its methods to get the wanted sort order.
But that would require some more thinking right now, so I can't give
code examples.

I take it this is documentation for something that has no other
documentation?

You mean the SPL and the example above? The SPL documentation is indeed
not the best, and the PHP manual even just shows a small part of it. The
official API reference is better, but still difficult to understand for
someone who hasn't used the SPL before.

Micha
.



Relevant Pages

  • Re: Recurse an array with an object/s
    ... And I have a class product ... Generraly I am sorting my objects inside arrays and I need to some how ... recurse those arrays to get to the objects. ... But if i do the pring inside the method like ...
    (comp.lang.php)
  • Recurse an array with an object/s
    ... Generraly I am sorting my objects inside arrays and I need to some how ... recurse those arrays to get to the objects. ... But if i do the pring inside the method like ...
    (comp.lang.php)