Renich Posted April 11, 2007 Share Posted April 11, 2007 Hello. I have a very specific problem and haven't been able to solve it. I have this array, which was created with a recursive funciton. The function finds a specified kind of file and lists the dirs in this way... The problem lies here: I need to the whole array to be 3 levels max: - 1st level containing an id or identifier (0, 1, 2, ...) - 2nd level containing the relative dir path (mp3, mp3/riffs, mp3/riffs/old, ...) - 3rd level containing an array with filenames and filepaths. Notes: - I may decide to include .txt files to add descriptions to each song. - It is important that the file_list function is recursive so it can get any level of subdirs easily. Them, I can sort my files in many cool ways like: lang/style/substyle/band/album/songs Please help me re-format this array. I'm almost there. I only need a cool way of taking "mp3/riffs" back to level 1 and we're there! Please remember that the funciton is recursive and that it will go on indefinitely on the sub levels. The first code block is the function used. The second is the array I have it. The third one is how I would like to have it. function list_get( $path, $lastdir = null ) { // scan $path if ( $files = scandir( $path ) ) { foreach( $files as $id => $filename ) { switch( $filename ) { // adios . case '.': unset( $files[$id] ); break; // adios .. case '..': unset( $files[$id] ); break; // if is dir, do it again case is_dir( "$path/$filename" ): if ( $lastdir !== null ) { // set name to directory at key and put the filelist inside it $files["$lastdir/$filename"] = list_get( "$path/$filename", $filename ); unset( $files[$id] ); } else { $files["$filename"] = list_get( "$path/$filename", $filename ); unset( $files[$id] ); } break; default: $files[$filename] = "$path/$filename"; unset( $files[$id] ); break; } } // return data return $files; } else { // error message die( "El directorio que proveiste no existe. {$_SERVER['SCRIPT_FILENAME']}" ); } } Array ( [mp3] => Array ( [song1.mp3] => path/to/song1.mp3 [song2.mp3] => path/to/song2.mp3 [song3.mp3] => path/to/song3.mp3 [mp3/riffs] => Array ( [riff1.mp3] => path/to/riff1.mp3 [riff2.mp3] => path/to/riff2.mp3 [riff3.mp3] => path/to/riff3.mp3 ) ) [ogg] => Array ( [song1.ogg] => path/to/song1.ogg [song2.ogg] => path/to/song2.ogg [song3.ogg] => path/to/song3.ogg [ogg/riffs] => Array ( [riff1.ogg] => path/to/riff1.ogg [riff2.ogg] => path/to/riff2.ogg [riff3.ogg] => path/to/riff3.ogg ) ) ) Array ( [mp3] => Array ( [song1.mp3] => path/to/song1.mp3 [song2.mp3] => path/to/song2.mp3 [song3.mp3] => path/to/song3.mp3 } [mp3/riffs] => Array ( [riff1.mp3] => path/to/riff1.mp3 [riff2.mp3] => path/to/riff2.mp3 [riff3.mp3] => path/to/riff3.mp3 ) [ogg] => Array ( [song1.ogg] => path/to/song1.ogg [song2.ogg] => path/to/song2.ogg [song3.ogg] => path/to/song3.ogg } [ogg/riffs] => Array ( [riff1.ogg] => path/to/riff1.ogg [riff2.ogg] => path/to/riff2.ogg [riff3.ogg] => path/to/riff3.ogg ) ) Quote Link to comment https://forums.phpfreaks.com/topic/46571-help-me-reformat-a-multi-level-array/ Share on other sites More sharing options...
sasa Posted April 11, 2007 Share Posted April 11, 2007 try <?php $a1 = Array ( 'mp3' => Array ( 'song1.mp3' => 'path/to/song1.mp3', 'song2.mp3' => 'path/to/song2.mp3', 'song3.mp3' => 'path/to/song3.mp3', 'mp3/riffs' => Array ( 'riff1.mp3' => 'path/to/riff1.mp3', 'riff2.mp3' => 'path/to/riff2.mp3', 'riff3.mp3' => 'path/to/riff3.mp3' ) ), 'ogg' => Array ( 'song1.ogg' => 'path/to/song1.ogg', 'song2.ogg' => 'path/to/song2.ogg', 'song3.ogg' => 'path/to/song3.ogg', 'ogg/riffs' => Array ( 'riff1.ogg' => 'path/to/riff1.ogg', 'riff2.ogg' => 'path/to/riff2.ogg', 'riff3.ogg' => 'path/to/riff3.ogg', 'ogg/riffs/old' => array( 'riff4.ogg' => 'path/to/riff4.ogg' ) ) ) ); function reorg($a, $b='') { $out =array(); foreach ($a as $key => $value) { if (!$b){ foreach ($value as $k1 => $v1) { if (!is_array($v1)) $out[$key][$k1] = $v1; else { $tmp = reorg($v1,$k1); foreach ($tmp as $k2 => $v2) $out[$k2] = $v2; } } } else { if (!is_array($value)) $out[$b][$key] = $value; else { $tmp = reorg($value,$key); foreach ($tmp as $k2 => $v2) $out[$k2] = $v2; } } } return $out; } print_r(reorg($a1)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/46571-help-me-reformat-a-multi-level-array/#findComment-226726 Share on other sites More sharing options...
Renich Posted April 12, 2007 Author Share Posted April 12, 2007 Hey, this works perfectly! Thanks a lot! I'll analize it a bit so I can really absorb it and understand it completely! Thanks again! I'll add credits at the script for you! ;=) Ummm... any website or email you would want me to add? Quote Link to comment https://forums.phpfreaks.com/topic/46571-help-me-reformat-a-multi-level-array/#findComment-227413 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.