Jump to content

Help me reformat a multi-level array!


Renich

Recommended Posts

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
         )
)

Link to comment
Share on other sites

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));
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.