Jump to content

Recommended Posts

I have a problem I am trying to solve involving an issue with sorting. I used a recursive function to draw a folder list from a directory that will constantly updated. So I get my list then I run through another function to find a stopping point... ie. a parent folder id (THE ONLY STATIC MARKER that I can use)....  once I have that I get something like management/ped/paint. Now my issue is I get several management/ped/build  and management/lat/build.  What I wanted to do was make a menu with tiers drop downs in it.... I've got that but having issues sorting the data as I want it..... so far I have this multi-dimensional array:

 

$count =  count($menu_array);
$count--; // Set Count again

/////////// DISPLAY IN MENU ORDER FOR LEVELS //////////////

while ($count >= 0)
{
	$i = 0;
	while($i < $high_count)
		{			
		echo $menu_array[$count][$i]."<br>";
			$i++;
		}
		echo "=====<br>";
	$count--;
}

 

Outputting this below, but I want it organized in a way to output it to a intranet navigation menu in drop down style tiers....  so newsletter , home,  events & home have no tiers... but management documents has several levels... level two being shipping & recieving, random, pedastal, & lateral... each with its appropiate level.... Help please... need logic idea to make this work.

Newsletter

DNE

DNE

=====

Managament Documents

Shipping & Recieving

Shipping

=====

Managament Documents

Shipping & Recieving

Recieving

=====

Managament Documents

Random

pp

=====

Managament Documents

Random

ff

=====

Managament Documents

Random

dd

=====

Managament Documents

Pedestal

DNE

=====

Managament Documents

Lateral

DNE

=====

Managament Documents

Engineering

DNE

=====

Home

DNE

DNE

=====

Help

DNE

DNE

=====

Events

DNE

DNE

=====

Link to comment
https://forums.phpfreaks.com/topic/189019-sorting-a-multidimensional-array/
Share on other sites

Give us the data you are working with (ie. the folder structure).

 

Then we can see how we can manipulate the data to be sorted properly.

 

Of course, any code given will be very basic and you will most likely have to adapt it to your specific needs.

 

-CB-

N/p I'm looking for something I can adapt and learn to use. Not a quick fix, so yes I understand what you mean.  Here's my beginning file structure (That will change nearly daily so have to deal with dynamic size and what not... so no static number of folders)

Events/

Help/

Home/

Managament Documents/Engineering/

Managament Documents/Lateral/

Managament Documents/Pedestal/

Managament Documents/Random/dd/

Managament Documents/Random/ff/

Managament Documents/Random/pp/

Managament Documents/Shipping & Recieving/Recieving/

Managament Documents/Shipping & Recieving/Shipping/

Newsletter/

Also almost forgot... I want to do this without a database...  :'(... but it is for a client that wants it that way. Wants to have everything in folders and just drop the folders or files in them without changing anything. Now... that is why I am doing the menu from file structure. I've got the methods to deal with before and after for this done or at least in the works. It's just the getting this folder listing in above posting to display in a format so I can insert it into a table.... div..... or as variables for a menu that has me stuck. Looking for ideas and methods since I am lost on how to do this suddenly.

it was a pretty interesting task. done some sample code and it worked out very well. But I used a file/dir hierechy system.

but if your just dealing with folders, than use recursion

routines i wrote up, have an array system of

$folder[] = array($foldername,$contents);

 

where $folder is the foldername, and $contents is a recursive call of the function returning an array.

 

The sort routine was pretty similar, it required recursion,

the first task was to seperate the $foldername from the $contents ($contents being an array of more folder contents)

the foldernames were just a simple array, the contents used the foldername as the index (key).

Sort the foldernames, than go thru them sorting the contents with a recursive call.

recombing them according to the sorted foldernames

 

there might be an easier way, but this was my first thoughts on accomplishing such a task

 

Awesome. That's what I was looking for I had been running into issues trying to loop it. Needed to get hit in head with a book, I had used a recursive call to begin with to get a list but but gave me that list above almost verbatim.... *sigh* well at least I know I could have saved myself headaches by stopping and thinking about changing that recursive function. Thnx! I'll let you know if it works out.

here is the sample code, I came up with

<?php

function build_dir($cdir='./')
{
$dh=opendir($cdir);
$dir=array();
while($file=readdir($dh))
{
	if(substr($file,0,1)=='.') continue; // if directory/file is . (self), .. (parent) .Filename (hidden). Ignore and continue
	if(is_dir($cdir.$file)) $dir[]=array($file,build_dir($cdir.$file));
	else $dir[]=$file;
}
return $dir;
}

function sort_dir(&$dir)
{
$subfolder=array();
foreach($dir as $key=>$file)
{
	if(is_array($file))
	{
		$subfolder[]=$file[0];
		$subcontents[$file[0]]=$file[1];
		unset($dir[$key]);
	}
}
sort($dir);
rsort($subfolder);
foreach($subfolder as $folder)
{
	sort_dir($subcontents[$folder]);
	array_unshift($dir,array($folder,$subcontents[$folder]));
}
return $dir;
}

function show_dir($dir,$level=0)
{
$indent=str_repeat(' ',$level*2);
foreach($dir as $file)
{
	if(is_array($file))
	{
		echo "{$indent}[{$file[0]}]\n";
		show_dir($file[1],$level+1);
	} else
		echo "{$indent}{$file}\n";
}
}


header('Content-Type: text/plain');
$dir=build_dir();
show_dir($dir);
echo "\n";
sort_dir($dir);
show_dir($dir);

 

This was my first thoughts on going about it, so I know a lot of optimization could be done.

But it works

Well I see where you are going. I'm still running into walls here. I'm already using some recursive code that gives me pretty much direct links to end of every folder. But looking at the sample code (And I haven't tested it, so correct me since I haven't played with that yet) and just looking at the sample code that's going to almost recursively call the same first set of folders is it not?

 

But your ideas is great and using that to try and modify it into mine.... sort of working but not as I want.

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.