Jump to content

Array into multidimensional array


vibes

Recommended Posts

Hi I have An array of file paths that I want to build into an array which I can then loop and display in a "file viewer", the problem been that I only want to output the folder name once and then the files within this folder. My solution is below but it doesnt work well once your 2+ subdirs in, the problem is if I have the same folder name deeper in the paths the second time it isnt output. Ideally a multidimensional array would work better that I can just lopp and print from $result['folder'] $result['files'] Iv tried this but when it comes to a subdir I hit a wall :( any help/pointers is very welcome iv lost 2 days on this and feel Like im just going around in circles now.

 

The array I have is, This is stored in a database with to columns filename and size, this is provided by the user as a string so I cant get the info myself sadly.

array (7)
{
'0' => string (15) test2/test2.txt
'1' => string (15) test1/test1.txt
'2' => string (33) test/test_test/test10/test101.txt
'3' => string (32) test/test_test/test10/test10.txt
'4' => string (23) test/test_test/test.txt
'5' => string (13) test/test.txt
'6' => string (9) test.root
}

 

This is the function iv currently got.

function getFiles($array, $size)
{
global $BASEPATH;
include($BASEPATH . '/include/fileTypes.php');
$i = 0;
$folderIcon = getFileTypeIcon('folder');
$string = '<div class="torDesc_fileTitle">Name<span class="torDesc_fileSizeTitle torDesc_right">Size</span></div>';
$string .='<div class="torDesc_file torDesc_fileAlt1 torDesc_folderName">' . $folderIcon . '</div>';
$lastCount = 0;
$lowerLevel = false;
$alt = 1;
//First we loop the files
foreach($array as $key => $value)
{
	++$i;
	$level = 0;
	//Split the path
	$keys = strpos($value, '/') !== false ? explode('/', $value) : array($value);
	$count = count($keys);
	$padding = 5;
	if($lastCount < $count)
		$upperLevel = true;
	else
		$upperLevel = false;

	//Now we loop each item in this path
	foreach($keys as $k => $v)
	{
		++$level;
		if($upperLevel && $level == $lastCount)
			$showFolder = true;
		else
			$showFolder = false;
		$folder = false;
		//First level folder
		if($level == 1 && $count !== 1)
		{
			//Only output the folder if we havent already done so.
			if($showFolder || !isset($output[md5($v . $level)]))
			{
				$repeater = $level;
				$tabs = '|' . str_repeat('_', ($repeater) * $padding);
				$alt = ($alt === 1 ? 2 : 1);
				$output[md5($v . $level)] = true;
				$string .= '<div class="torDesc_file torDesc_fileAlt' . $alt . '">  ' . $tabs . '<span class="torDesc_folderName"> ' . $folderIcon . ' ' . htmlentities($v) . 'a</span></div>';

			}
		}
		//Second+ Level folder
		elseif($level != $count)
		{
			//Only output the folder if we havent already done so.
			if($showFolder || !isset($output[md5($v . $level)]))
			{
				$folder = true;
				$repeater = $level - 1;
				$tabs = '|' . str_repeat(str_repeat(' ', $padding * 2) . '|', $repeater) . str_repeat('_', $padding);
				$alt = ($alt === 1 ? 2 : 1);
				$output[md5($v . $level)] = true;
				$string .= '<div class="torDesc_file torDesc_fileAlt' . $alt . '">  ' . $tabs . '<span class="torDesc_folderName"> ' . $folderIcon . ' ' . htmlentities($v) . 'b</span></div>';
			}
		}
		//Its a file
		else
		{
			$alt = ($alt === 1 ? 2 : 1);
			$ext = explode('.', $v);
			$ext = strtolower($ext[count($ext) -1]);
			$img = getFileTypeIcon($ext);
			$repeater = $level - 1;
			$tabs = '|' . str_repeat(str_repeat(' ', $padding * 2) . '|', $repeater) . str_repeat('_', $padding);
			$string .=  '<div class="torDesc_file torDesc_fileAlt' . $alt . '">  ' . $tabs . '<span class="torDesc_fileName"> ' . $img . ' ' . htmlentities($v) . 'c</span><span class="torDesc_fileSize">' . mksize($size[($i -1)]) . '</span></div>';
		}
	}
	$lastCount = $count;
}
return $string;
}

 

Link to comment
Share on other sites

The array I have is, This is stored in a database with to columns filename and size, this is provided by the user as a string so I cant get the info myself sadly.

 

I think it would be a lot easier for you if the data is stored in two tables and for you to access it. If you can access the database directly, the following would make more sense:

 

`directories`(

`refno` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,

`path` VARCHAR(10000) NOT NULL

)

 

`files`(

`refno` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,

`directory_ref` INT NOT NULL,

`name` VARCHAR(10000) NOT NULL,

`size` INT NOT NULL

)

 

If you do this, you can then run a JOIN to link all files to their relevant directory

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.