Well, this definitely is some code that stumps me. I'll post my source, then what I input, then what I want outputted. <?php ob_start(); class Index { # Current Directory public $dir; # File List public $file_array; # Directory Buffer public $buffer; # When the class is loaded... function __construct() { # Set the directory. $this->dir = getcwd(); # Put the search into a buffer. $this->file_array = $this->recursive_search($this->dir); } # Start the script. function start() { $this->retrieve_data($this->file_array); } function recursive_search($source_dir) { if($fp = @opendir($source_dir)) { $source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; $filedata = array(); while (FALSE !== ($file = readdir($fp))) { if ((strncmp($file, '.', 1) == 0) OR ($file == '.' OR $file == '..')) { continue; } if(@is_dir($source_dir.$file)) { $temp = array(); $temp = $this->recursive_search($source_dir.$file.DIRECTORY_SEPARATOR); $filedata[$file] = $temp; } else { $filedata[] = $file; } } closedir($fp); return $filedata; } else { return FALSE; } } /**************************** * THIS IS WHERE THE ISSUE IS! ****************************/ function retrieve_data($file_array) { foreach ($file_array as $key => $value) { $this->buffer = rtrim($key, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; foreach( $value as $k => $v ) { $this->buffer = $this->buffer.rtrim($k, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; foreach ($v as $a => $b) { echo $this->buffer.$b."\n\n"; } } } } /**************************** * END OF THE ISSUE! ****************************/ } # Run on load. $index = new Index; # For testing purposes.. load this data: $index->file_array = array( 'AFI' => array( 'Album 1' => array( '0' => 'Song 1.mp3', '1' => 'Song 2.mp3', '2' => 'Song 3.mp3' ), 'Album 2' => array( '0' => 'Song 1.mp3', '1' => 'Song 2.mp3', '2' => 'Song 3.mp3' ) ), 'Arctic Monkeys' => array( 'Album 1' => array( '0' => 'Song 1.mp3', '1' => 'Song 2.mp3', '2' => 'Song 3.mp3' ), 'Album 2' => array( '0' => 'Song 1.mp3', '1' => 'Song 2.mp3', '2' => 'Song 3.mp3' ) ) ); $index->start(); # Output everything. $source = ob_get_contents(); ob_end_clean(); echo '<pre>',$source,'</pre>'; /** END OF FILE **/ PHP: THE EXAMPLE INPUT: # For testing purposes.. load this data: $index->file_array = array( 'AFI' => array( 'Album 1' => array( '0' => 'Song 1.mp3', '1' => 'Song 2.mp3', '2' => 'Song 3.mp3' ), 'Album 2' => array( '0' => 'Song 1.mp3', '1' => 'Song 2.mp3', '2' => 'Song 3.mp3' ) ), 'Arctic Monkeys' => array( 'Album 1' => array( '0' => 'Song 1.mp3', '1' => 'Song 2.mp3', '2' => 'Song 3.mp3' ), 'Album 2' => array( '0' => 'Song 1.mp3', '1' => 'Song 2.mp3', '2' => 'Song 3.mp3' ) ) ); PHP: What I'd like it to parse too: AFI\Album 1\Song 1.mp3 AFI\Album 1\Song 2.mp3 AFI\Album 1\Song 3.mp3 AFI\Album 2\Song 1.mp3 AFI\Album 2\Song 2.mp3 AFI\Album 2\Song 3.mp3 Arctic Monkeys\Album 1\Song 1.mp3 Arctic Monkeys\Album 1\Song 2.mp3 Arctic Monkeys\Album 1\Song 3.mp3 Arctic Monkeys\Album 2\Song 1.mp3 Arctic Monkeys\Album 2\Song 2.mp3 Arctic Monkeys\Album 2\Song 3.mp3 Code (markup): ---------------------------------------------- The challenge to this program is that it needs to display properly for each directory. So if there are more (or less) directories with files, it must be able to sort through everything without having issues. Cheers
that's easy by doing foreach() foreach ($file_array as $kn => $dir) { if(is_array($dir)) { foreach ($dir as $k => $v) { foreach ($v as $song) { print "$kn/$k/"; print $song."<br/>"; } } } } PHP:
I know it involves foreach loops, but I need it to infinitely recurse through the directory. Works for: / /afi /afi/album 1/ /afi/album 2/more/dir/in/here
Try this method function retrieve_data($file_array,$prefix='') { foreach ($file_array as $key => $value) { if (is_array($value)) { $this->retrieve_data($value,$prefix.rtrim($key, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR); } else { echo $prefix.$value."\n"; } } } PHP: $index->retrieve_data($index->file_array); PHP: