+7  A: 

I recommend using DirectoryIterator to build your array

Here's a snippet I threw together real quick, but I don't have an environment to test it in currently so be prepared to debug it.

$fileData = fillArrayWithFileNodes( new DirectoryIterator( '/path/to/root/' ) );

function fillArrayWithFileNodes( DirectoryIterator $dir )
{
  $data = array();
  foreach ( $dir as $node )
  {
    if ( $node->isDir() && !$node->isDot() )
    {
      $data[$node->getFilename()] = fillArrayWithFileNodes( new DirectoryIterator( $node->getPathname() ) );
    }
    else if ( $node->isFile() )
    {
      $data[] = $node->getFilename();
    }
  }
  return $data;
}
Peter Bailey
I'm getting this error: Fatal error: Maximum function nesting level of '100' reached, aborting!And I don't see why I should get this...highest nesting I have is 5 levels.
WebDevHobo
Ok, I got a chance to run this and fixed the bugs - give it another shot.
Peter Bailey
What happens now is that whenever I go to a new primary folder, the old ones get overwritten.
WebDevHobo
Your original questions asks how to recursively walk over a directory tree and load that data into an array - my answer does that. If you're expecting something more you're going to have to provide more detail.
Peter Bailey
Agreed. I drew an example on a white-board and took a picture, you can view it in the original post. I've been working on my own solution and I'm kinda beginning to think that what I'm looking for is impossible.
WebDevHobo
Peter's solution looks like it does exactly what your whiteboard shows. What's the problem?
rojoca
@rojoca: what happen when Peter's function is called, is that the array that is returned is the very last set of pics from the very last album. All the previous ones have been overwritten.
WebDevHobo
Using the pic as an example, suppose there is only Paris and Barcelona, the returned array would contain Paris and sub-maps, but not Barcelona, which is actually overwritten by Paris.
WebDevHobo
When I tried using the function it worked perfectly. Are you using as follows: $fileData = fillArrayWithFileNodes(new DirectoryIterator('/around-the-world/'));var_dump($fileData);
rojoca
Hmz, the var dump shows the array, my debugger does not...Anyways, still one problem, it only goes 2 levels deep in folder structure... I'm gonna try and solve that, but any ideas are welcome.
WebDevHobo
Also, what about foreign characters? Like äää
WebDevHobo
A: 

A simple implementation without any error handling:

function dirToArray($dir) {
    $contents = array();
    # Foreach node in $dir
    foreach (scandir($dir) as $node) {
        # Skip link to current and parent folder
        if ($node == '.')  continue;
        if ($node == '..') continue;
        # Check if it's a node or a folder
        if (is_dir($dir . DIRECTORY_SEPARATOR . $node)) {
            # Add directory recursively, be sure to pass a valid path
            # to the function, not just the folder's name
            $contents[$node] = dirToArray($dir . DIRECTORY_SEPARATOR . $node);
        } else {
            # Add node, the keys will be updated automatically
            $contents[] = $node;
        }
    }
    # done
    return $contents;
}
soulmerge
What happens here is that the directory listing is flattened and all files are listed right after one another.
WebDevHobo
I just ran the on part of my music compilation, works as expected.
soulmerge
A: 

I've had success with the PEAR File_Find package, specifically the mapTreeMultiple() method. Your code would become something like:

require_once 'File/Find.php';
$fileList = File_Find::mapTreeMultiple($dirPath);

This should return an array similar to what you're asking for.

Michael Johnson
What's this 'File/Find.php' file? I've scanned my harddrive and I have no such file on it. I do have PEAR installed. Might be that I'm using Windows.
WebDevHobo