hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Array To Tree View
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

Array To Tree View

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 12-20-2010, 06:39 AM
latheesan latheesan is offline
AsuraHosting.Com
 
Join Date: Jan 2005
Location: UK, London
Posts: 762
Question

Array To Tree View


Hi,

I have a php script which connects to a given FTP account and returns the recursive folder structure like this:

PHP Code:
$structure = array (
  
=>
  array (
    
'dirName' => 'first dir',
    
'children' =>
    array (
      
=>
      array (
        
'dirName' => 'first sub dir',
        
'children' =>
        array (
          
=>
          array (
            
'dirName' => 'first sub-sub dir',
            
'children' => NULL,
          ),
        ),
      ),
      
=>
      array (
        
'dirName' => 'second sub dir',
        
'children' =>
        array (
          
=>
          array (
            
'dirName' => 'first sub-sub dir',
            
'children' =>
            array (
              
=>
              array (
                
'dirName' => 'first sub-sub-sub dir',
                
'children' => NULL,
              ),
            ),
          ),
          
=>
          array (
            
'dirName' => 'second sub-sub dir',
            
'children' => NULL,
          ),
        ),
      ),
      
=>
      array (
        
'dirName' => 'third sub dir',
        
'children' =>
        array (
          
=>
          array (
            
'dirName' => 'first sub-sub',
            
'children' =>
            array (
              
=>
              array (
                
'dirName' => 'first sub-sub-sub dir',
                
'children' => NULL,
              ),
            ),
          ),
        ),
      ),
    ),
  ),
  
=>
  array (
    
'dirName' => 'second dir',
    
'children' =>
    array (
      
=>
      array (
        
'dirName' => 'first sub dir',
        
'children' =>
        array (
          
=>
          array (
            
'dirName' => 'first sub-sub dir',
            
'children' => NULL,
          ),
        ),
      ),
    ),
  ),
  
=>
  array (
    
'dirName' => 'third dir',
    
'children' =>
    array (
      
=>
      array (
        
'dirName' => 'first sub dir',
        
'children' => NULL,
      ),
    ),
  ),
); 
I want to display the above array in this format:

Quote:
first dir = /first dir
- first sub dir = /first dir/first sub dir
-- first sub-sub dir = /first dir/first sub dir/first sub-sub dir
- second sub dir = /first dir/second sub dir

etc...
The code i have so far is:

PHP Code:
function TreeView($array$index 0)
{
    global 
$tree$arr_log;

    
$space '';
    for (
$i 0$i $index$i++) {
        
$space .= '-';
    }

    foreach (
$array as $folder)
    {
        
$tree .= $space .' '$folder['dirName'] .'<br />';

        if (
is_array($folder['children'])) {
            
$index++;
            
$arr_log[$folder['dirName']] = $index;
            
TreeView($folder['children'], $index);
        }

        if (
$arr_log[$folder['dirName']] > 0) {
            
$index--;
        }
    }
}

$arr_log = array();
$tree "";
TreeView($structure);
echo 
$tree
The above code only prints the following:

Quote:
first dir
- first sub dir
-- first sub-sub dir
- second sub dir
-- first sub-sub dir
--- first sub-sub-sub dir
-- second sub-sub dir
- third sub dir
-- first sub-sub
--- first sub-sub-sub dir
second dir
- first sub dir
-- first sub-sub dir
third dir
- first sub dir
How can i modify it, so every time a sub-dir is printed, it prints the full path to it using parent dirs?

__________________
:: www.AsuraHosting.com ::


Last edited by latheesan; 12-20-2010 at 06:52 AM.
Reply With Quote


Sponsored Links
  #2  
Old 12-20-2010, 09:55 AM
latheesan latheesan is offline
AsuraHosting.Com
 
Join Date: Jan 2005
Location: UK, London
Posts: 762
Here's another example, in-case my previous one was not clear.

Assume the folder structure is in an array like this:

PHP Code:
$structure = array (
  
=>
  array (
    
'dirName' => 'apple',
    
'children' =>
    array (
      
=>
      array (
        
'dirName' => 'apple desert',
        
'children' =>
        array (
          
=>
          array (
            
'dirName' => 'apple desert incrediants',
            
'children' =>
            array (
              
=>
              array (
                
'dirName' => 'apple desert incrediants altered',
                
'children' => NULL,
              ),
            ),
          ),
        ),
      ),
      
=>
      array (
        
'dirName' => 'apple juice',
        
'children' =>
        array (
          
=>
          array (
            
'dirName' => 'apple juice incrediants',
            
'children' => NULL,
          ),
        ),
      ),
      
=>
      array (
        
'dirName' => 'apple pie',
        
'children' =>
        array (
          
=>
          array (
            
'dirName' => 'apple pie incrediants',
            
'children' => NULL,
          ),
        ),
      ),
    ),
  ),
  
=>
  array (
    
'dirName' => 'banana',
    
'children' =>
    array (
      
=>
      array (
        
'dirName' => 'banana desert',
        
'children' =>
        array (
          
=>
          array (
            
'dirName' => 'banana desert incrediants',
            
'children' => NULL,
          ),
        ),
      ),
      
=>
      array (
        
'dirName' => 'banana juice',
        
'children' =>
        array (
          
=>
          array (
            
'dirName' => 'banana juice incrediants',
            
'children' => NULL,
          ),
        ),
      ),
      
=>
      array (
        
'dirName' => 'banana pie',
        
'children' =>
        array (
          
=>
          array (
            
'dirName' => 'banana pie incrediants',
            
'children' => NULL,
          ),
        ),
      ),
    ),
  ),
  
=>
  array (
    
'dirName' => 'cranberry',
    
'children' =>
    array (
      
=>
      array (
        
'dirName' => 'cranberry desert',
        
'children' =>
        array (
          
=>
          array (
            
'dirName' => 'cranberry desert incrediants',
            
'children' => NULL,
          ),
        ),
      ),
      
=>
      array (
        
'dirName' => 'cranberry juice',
        
'children' =>
        array (
          
=>
          array (
            
'dirName' => 'cranberry juice incrediants',
            
'children' => NULL,
          ),
        ),
      ),
      
=>
      array (
        
'dirName' => 'cranberry pie',
        
'children' =>
        array (
          
=>
          array (
            
'dirName' => 'cranberry pie incrediants',
            
'children' => NULL,
          ),
        ),
      ),
    ),
  ),
); 
I am trying to parse the above array and display it like this (don't require color coding - that's just there so you can visually follow the structure):

Quote:
apple = /apple
- apple desert = /apple/apple desert
-- apple desert incrediants = /apple/apple desert/apple desert incrediants
--- apple desert incrediants altered = /apple/apple desert/apple desert incrediants/apple desert incrediants altered
- apple juice = /apple/apple juice
-- apple juice incrediants = /apple/apple juice/apple juice incrediants
- apple pie = /apple/apple pie
-- apple pie incrediants = /apple/apple pie/apple pie incrediants

etc...
The function TreeView i have now, is only able to list the folder structure like this:

Quote:
apple
- apple desert
-- apple desert incrediants
--- apple desert incrediants altered
- apple juice
-- apple juice incrediants
- apple pie
-- apple pie incrediants
banana
- banana desert
-- banana desert incrediants
- banana juice
-- banana juice incrediants
- banana pie
-- banana pie incrediants
cranberry
- cranberry desert
-- cranberry desert incrediants
- cranberry juice
-- cranberry juice incrediants
- cranberry pie
-- cranberry pie incrediants
What i am trying to achieve is have the full recursive path next to every folder (like in my example above).

any ideas?

__________________
:: www.AsuraHosting.com ::

Reply With Quote
  #3  
Old 12-22-2010, 08:08 AM
latheesan latheesan is offline
AsuraHosting.Com
 
Join Date: Jan 2005
Location: UK, London
Posts: 762
Never mind, I figured it out:

PHP Code:
function TreeView($array$index 0$parent '/')
{
    global 
$tree$arr_log;

    
$space '';
    for (
$i 0$i $index$i++) {
        
$space .= '-';
    }

    if (
$space != '') {
        
$dirPath $parent;
    }

    foreach (
$array as $folder)
    {
        
$dirName $folder['dirName'];

        
$tree .=  $dirName .' = '$dirPath .'/'$dirName .'<br />';

        if (
is_array($folder['children'])) {
            
$index++;
            
$arr_log[$dirName] = $index;
            
TreeView($folder['children'], $index$dirPath .'/'$dirName);
        }

        if (
$arr_log[$dirName] > 0) {
            
$index--;
        }
    }


__________________
:: www.AsuraHosting.com ::

Reply With Quote
Sponsored Links
Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
OS Drive + Array or OS + Array Drives peruviantalk Colocation and Data Centers 11 06-04-2009 05:59 PM
View all IP'S of cPanel login's, View FTP log in information Duport Hosting Security and Technology 11 02-25-2005 08:32 PM
Binary tree to preorder traversal to Binary tree Cyrus Programming Discussion 2 11-22-2004 08:17 AM
You've heard of a family tree, well here's the town name tree. Critic Web Hosting Lounge 9 11-25-2003 07:54 PM
java~converting array to array list gracie Programming Discussion 13 04-29-2003 02:19 AM

Related posts from TheWhir.com
Title Type Date Posted
VMware Expands on Hybrid Cloud-based Desktop, Application Tools Web Hosting News 2012-05-02 16:34:00
Web Host webHOSTING.net Improves Data Backup Using Nimble Storage Array Web Hosting News 2012-03-28 13:14:22
Hosts are the Most at theWHIR Networking Event Chicago Blog 2012-03-16 16:14:28
Security Firm Prolexic Launches Online Resource Portal for DDoS Mitigation Web Hosting News 2012-01-19 12:55:48
Cloud Host GoGrid Deploys Modius OpenData Infrastructure Management Web Hosting News 2011-10-31 17:40:00


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?