Jump to content

Recusive function issues


gwolgamott

Recommended Posts

Having issues setting up my recursion... and too tired apparently to think straight. So bare with me since I think I have a logic issue now. Help I need to find how to make this work recursively. But a brief explanation of what the var are:

$menu_array is an array of the first set of folders in a large directory (see below)

Array ( [0] => Events [1] => Help [2] => Home [3] => Managament Documents [4] => Newsletter ) 

$stopper is used to tell the build_dir function whether or not to return a folder list or just an array with element \[0\] = NULL

$link is a directory list.

$menu_count is just the element count of $menu_array

 

function sort_array($menu_count, $menu_array, $link, $stopper)
{
////////////////////////////////
// $menu_count is number of first set of arrays
// $menu_array is the first set of arrays
// $link is the intial starting location
// $stopper is the stopper file
///////////////////////////////
for($i=0;$i<$menu_count;$i++)
{
	$element = $menu_array[$i];
	$link = "../index/".$element."/";
	$menu_array_2 = build_dir($link, $stopper);

	if ($menu_array_2[0] == NULL)
		{
			echo $link." : ";

			$menu_array_3[$element] = $menu_array_2;
			print_r ($menu_array_2); echo "<br>";
			//echo "NULL"; // for testing only
		}
	if ($menu_array_2[0]!= NULL)
		{
			echo $link." : ";
			print_r ($menu_array_2); echo "<br>";
/////////////////////////////////////THE LINE BELOW IS WHAT I NEED TO TRY TO WORK///////////////
			//$menu_array_3[$element] = sort_array($menu_count, $menu_array, $link, $stopper);
		}	
	$menu_array_2 = array(); // destroys temp array
	}
print_r($menu_array_3); echo "<br>";
return $menu_array_3;
}

gives the current Test output of at the moment:

../index/Events/ : Array ( [0] => New Folder ) 
../index/Help/ : Array ( [0] => ) 
../index/Home/ : Array ( [0] => ) 
../index/Managament Documents/ : Array ( [0] => Engineering [1] => Lateral [2] => Pedestal [3] => Random [4] => Shipping & Recieving ) 
../index/Newsletter/ : Array ( [0] => ) 
Array ( [Help] => Array ( [0] => ) [Home] => Array ( [0] => ) [Newsletter] => Array ( [0] => ) ) 

 

and my build_dir function if want to know what that is:

<?php
function build_dir($cdir,$stopper)
{
$cdir = $cdir;
$stopper = $stopper;
//echo $cdir."<br>";
   $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[]=$file;
      }
      else
      {
      	if($file == "struct.php")	
      	{
   			//echo $file."<br>";
   			$dir = array(); //DESTROY THE ARRAY IF THERE IS EVER A STRUCT FILE IN THE DIRECTORY
   			$dir[0] = NULL;
   			break;
   		}
   	  }
}
   return $dir;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/190241-recusive-function-issues/
Share on other sites

slight infinite loop typo I know I did make...

 

//$menu_array_3[$element] = sort_array($menu_count, $menu_array, $link, $stopper);

 

that line supposed to be something like

 

$menu_array_3[$element] = sort_array($menu_count, $menu_array_2, $link, $stopper);

 

but that is still not right and have myself lost

I solved it... not sure wth I was thinking but just retrofitted a tree travesal file to make need of what I wanted... I'll post the solution I made myself when it is done if anyone is interested. Sorry and thanks to those that read the posts.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.