Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    array structure problem

    Hello all

    I am building a directory tree function, I wanna put my directory structure into an array but cannot wrap my finger around it so far, can anyone help?

    Here is an example of directory structure I would like to put in such an array

    0 test
    ___1 yoyou34
    0 yoyo
    ___1 obladi oblada
    0 yoyou
    0 testghj
    ___1 balloon
    0 images
    ___1 test
    ______2 Gisele
    ___1 tralala
    ___1 circus
    ______2 clowns
    _________3 forks
    _________3 balloons
    ____________4 monkeys

    The underscores represent spaces, to help visualize the tree and the numbers are tree levels

    Thanks for any help!

    GCharb
    Last edited by GCharb; 06-10-2007 at 03:28 PM.
    If you have nothing good to say about someone, say nothing at all!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Recursion is the only way that I know of to build a directory structure.
    I'll see if I can dig up an example later, but for now I'm too tired to try, lol
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    72
    Thanks
    0
    Thanked 2 Times in 2 Posts
    You could use an associative array per directory where keys are names, therefore subdirectories are merely other arrays.

    eg.
    PHP Code:
    $d = array(
      
    'test' = array(
        
    'yoyou34' = array()
      ),
      
    'yoyo' = array(
        
    'obladi oblada' = array()
      ),
      
    'yoyou' = array()
    ); 
    If you need recursive directory navigation you'll need to use the php directory functions.
    Every PHP programmer of any skill level should set error_reporting(E_ALL); at the top of their scripts or in their php.ini

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That only works with hardcoding the values. The recursion takes care of automatic assignment.
    Granted I'm sure I've seen another way of constructing a tree, but recursion is the only way I've done it in the past.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    New Coder
    Join Date
    May 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks to both of you for the quick reply

    Yeah, I use a recursive directory function but I am not sure on how to include the data in a usefull array.

    I tried a multidimensional array but cant make it work so far!

    Here is the code for the recursive directory function I made

    <?

    function getDirectory( $path = '.', $level = 0 )
    {
    $ignore = array( 'cgi-bin', '.', '..' );
    $dirArray = array();

    $dh = @opendir( $path );

    while( false !== ( $file = readdir( $dh ) ) )
    {
    if( !in_array( $file, $ignore ) )
    {
    $spaces = str_repeat( '&nbsp;', ( $level * 4 ) );
    if( is_dir( "$path/$file" ) )
    {
    echo "$spaces $file<br />\n";
    $count++;
    getDirectory( "$path/$file", ($level+1) );
    }
    }
    }

    closedir( $dh );
    }

    $dir = getDirectory( "root/files" );

    ?>
    GCharb
    Last edited by GCharb; 06-10-2007 at 03:43 PM.
    If you have nothing good to say about someone, say nothing at all!

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Its tricky work for sure, if you store the data in a multidimensional array, you also need to extract it recursively from the array. I suppose you can add a depth level to it as well, but then you would construct loops based on depth and that idea doesn't sound too appealing.
    Your on the right track though, keep playing with your array merges and parent/children values. Check google too, surely someone has an example posted up
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    New Coder
    Join Date
    May 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello again!

    Yeah, found many on google and codingforums but all put the data in a one dimesion array, not usefull for me.

    I can use the $level variable to get a levl for each root directory and up, but where I get headaches is when the directories get branches, that s where it gets complicated!

    GCharb
    If you have nothing good to say about someone, say nothing at all!

  • #8
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Your function GCharb is similar to one found here

    http://www.codingforums.com/showthread.php?t=71882

  • #9
    New Coder
    Join Date
    May 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yup, that s the one, plenty of those all over the web

    Has no tools for arrays though!
    If you have nothing good to say about someone, say nothing at all!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •