Forum Moderators: coopster
function getDirectoryTree($dir){
$handle = opendir($dir);
for(;(false!== ($readdir = readdir($handle)));)
{
if($readdir!= '.' && $readdir!= '..'){
$path = $dir.'/'.$readdir;
if(is_dir($path)) $output[$readdir] = getDirectoryTree($path);
if(is_file($path)) $output[] = $readdir;
}
}
return isset($output)?$output:false;
closedir($handle);
}
the print_r of the array looks like:
Array ( [0] => Array ( [Dogs] => Array ( [Puppies] => Array ( [0] => 0165.JPG [1] => 0109.JPG [2] => 0522.JPG [3] => 0299.JPG [4] => 0217.JPG ) [Adult] => Array ( [0] => 0420.JPG [1] => 0408.JPG [2] => 004112.jpg [3] => 002709.jpg [4] => 002705.jpg [5] => 02609.jpg [6] => 002586.jpg ) ) [Cats] => Array ( [0] => 0674.JPG [1] => 666.JPG [2] => 0600.JPG [3] => 0574.JPG [4] => 0529.JPG [5] => 3975.jpg [6] => 0746.JPG ) [Multishots] => Array ( [0] => 0600.JPG [1] => 666.JPG [2] => 0674.JPG [3] => 0746.JPG [4] => 3975.jpg ) [Graphics] => Array ( [0] => 0600.JPG [1] => 666.JPG [2] => 0674.JPG [3] => 0746.JPG [4] => 3975.jpg ) [Products] => Array ( [0] => 0600.JPG [1] => 666.JPG [2] => 0674.JPG [3] => 0746.JPG [4] => 3975.jpg ) [Test] => ) )
i am trying to output a simple list of the root directorys from the array like this:
foreach ($navTree[0] as $menu)
{
print $menu . '<br />';
}
doesnt work,it only outputs Array, when i guess what i need to know is how to access the name of the array?
thanks
jonathan
you could build multiple arrays in your function
my honest advice is you are going to give yourself a headache making arrays that complex. My head hurts just looking at it ;)
another note, use
echo '<pre>';
print_r here
echo '</pre>';
this will make your recursive prints much easier to read ;)
so my function provides me with the root folder contents... and makes an array of that...
so my plan is to count the length of the mainNav array, then go through each item and make an array of its contents... i want to be able to call the getDir function but not sure about how to go about the array_push method because i want to go create and push an array that is named a variable... so for example:
array_push ($variableContainingTheArrayName, $item);
where:
$variableContainingTheArrayName = $mainNav[0];
is that even possible?
thanks for your time and wisdom,
its really appreciated...
jonathan
function getDir($path)
{
global $mainNav;
if ($handle = opendir($path))
{
while (false!== ($file = readdir($handle)))
{
if ($file!= "." && $file!= "..")
{
array_push($mainNav, $file);
}
}
closedir($handle);
}
}
$mystring = 'myarrayname';
${$mystring} = array('stuff1','stuff2');
variable variables [php.net] one of my favourites ;)
<?php
require_once 'ibConfig.php';
Dir2Array('mainNav', PATH);
function Dir2Array($arrayName, $path)
{
${$arrayName} = array();
global ${$arrayName};
if ($handle = opendir($path))
{
while (false!== ($file = readdir($handle)))
{
if ($file!= "." && $file!= "..")
{
array_push (${$arrayName}, $file);
}
}
closedir($handle);
}
}
print_r (${$arrayName});
?>
the array_push doesnt like my syntax...
so i was researching variable variables and you can have variable variable variables etc... hehehe
crazy ;)
should i just get you to write the program 4 me? hehehe
thx again.
j.
function Dir2Array($arrayName, $path)
{
global $$arrayName;
$$arrayName = array();
if ($handle = opendir($path))
{
while (false!== ($file = readdir($handle)))
{
if ($file!= "." && $file!= "..")
{
array_push ($$arrayName, $file);
}
}
closedir($handle);
}
}
Dir2Array('mainNav', PATH);
foreach ($mainNav as $subNav)
{
Dir2Array($subNav, PATH . $subNav);
}
jonathan