Forum Moderators: coopster

Message Too Old, No Replies

multi dimension array accessing

         

sneaks

11:38 pm on Sep 13, 2005 (gmt 0)

10+ Year Member



hi. having problems accessing a multi dimensional array of a directory tree... heres the function:


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

jatar_k

1:26 am on Sep 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you just want the root dirs why not build a simple array of them for that part?

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

sneaks

2:00 am on Sep 14, 2005 (gmt 0)

10+ Year Member



jatar,
i do need the subdirs as well, i am determined to build a collapsible navigation for my website...
j.

jatar_k

2:11 am on Sep 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what I am saying is it doesn't necessarily have to all be in the same complex array

you could use multiple arrays

one with cats then either one with each subcat having a matching key to the first or individual arrays named for the cats

I just find them easier to debug that way

jatar_k

2:18 am on Sep 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



is this the right structure?

Array (  
[0] => Array (
[Dogs] => Array (
[Puppies] => Array ()
[Adult] => Array ()
)
[Cats] => Array ()
[Multishots] => Array ()
[Graphics] => Array ()
[Products] => Array ()
[Test] => ) )

so a single complex array in the one row, with only the one set of subcats?

sneaks

2:38 am on Sep 14, 2005 (gmt 0)

10+ Year Member



yes...
you're right about making it more complex than necassry since one sub category deep is all i am concerned with...

sneaks

3:01 am on Sep 14, 2005 (gmt 0)

10+ Year Member



hey j & all,
okay, so inline with your advice, i will make multiple arrays... it is definitely more solid... especially since i am trying to learn this...

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

sneaks

3:23 am on Sep 14, 2005 (gmt 0)

10+ Year Member



i guess what i am asking is it possible to make a string's value the name a new string or in this case an array?

jatar_k

3:46 am on Sep 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



definitely

$mystring = 'myarrayname';
${$mystring} = array('stuff1','stuff2');

variable variables [php.net] one of my favourites ;)

sneaks

3:51 am on Sep 14, 2005 (gmt 0)

10+ Year Member



man that is sooooo cool...

thanks j.

jatar_k

3:56 am on Sep 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hehe, my pleasure

sneaks

4:23 am on Sep 14, 2005 (gmt 0)

10+ Year Member



yo, i feel like a dork asking for so much help but please please i'm beggin ya... :)


<?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.

sneaks

4:28 am on Sep 14, 2005 (gmt 0)

10+ Year Member



got it,

should be:


$$arrayName = array();

sneaks

4:41 am on Sep 14, 2005 (gmt 0)

10+ Year Member



if youre interested,
heres the code its a lot more simplifed thats for sure...

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

jatar_k

2:39 pm on Sep 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> should be:

both syntaxes work, I use the braces just because i find it easier to read and concatenating strings for var names needs braces.

nice work figuring it all out