
12-20-2010, 06:39 AM
|
|
AsuraHosting.Com
|
|
Join Date: Jan 2005
Location: UK, London
Posts: 762
|
|
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 ( 0 => array ( 'dirName' => 'first dir', 'children' => array ( 0 => array ( 'dirName' => 'first sub dir', 'children' => array ( 0 => array ( 'dirName' => 'first sub-sub dir', 'children' => NULL, ), ), ), 1 => array ( 'dirName' => 'second sub dir', 'children' => array ( 0 => array ( 'dirName' => 'first sub-sub dir', 'children' => array ( 0 => array ( 'dirName' => 'first sub-sub-sub dir', 'children' => NULL, ), ), ), 1 => array ( 'dirName' => 'second sub-sub dir', 'children' => NULL, ), ), ), 2 => array ( 'dirName' => 'third sub dir', 'children' => array ( 0 => array ( 'dirName' => 'first sub-sub', 'children' => array ( 0 => array ( 'dirName' => 'first sub-sub-sub dir', 'children' => NULL, ), ), ), ), ), ), ), 1 => array ( 'dirName' => 'second dir', 'children' => array ( 0 => array ( 'dirName' => 'first sub dir', 'children' => array ( 0 => array ( 'dirName' => 'first sub-sub dir', 'children' => NULL, ), ), ), ), ), 2 => array ( 'dirName' => 'third dir', 'children' => array ( 0 => 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?
Last edited by latheesan; 12-20-2010 at 06:52 AM.
|

12-20-2010, 09:55 AM
|
|
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 ( 0 => array ( 'dirName' => 'apple', 'children' => array ( 0 => array ( 'dirName' => 'apple desert', 'children' => array ( 0 => array ( 'dirName' => 'apple desert incrediants', 'children' => array ( 0 => array ( 'dirName' => 'apple desert incrediants altered', 'children' => NULL, ), ), ), ), ), 1 => array ( 'dirName' => 'apple juice', 'children' => array ( 0 => array ( 'dirName' => 'apple juice incrediants', 'children' => NULL, ), ), ), 2 => array ( 'dirName' => 'apple pie', 'children' => array ( 0 => array ( 'dirName' => 'apple pie incrediants', 'children' => NULL, ), ), ), ), ), 1 => array ( 'dirName' => 'banana', 'children' => array ( 0 => array ( 'dirName' => 'banana desert', 'children' => array ( 0 => array ( 'dirName' => 'banana desert incrediants', 'children' => NULL, ), ), ), 1 => array ( 'dirName' => 'banana juice', 'children' => array ( 0 => array ( 'dirName' => 'banana juice incrediants', 'children' => NULL, ), ), ), 2 => array ( 'dirName' => 'banana pie', 'children' => array ( 0 => array ( 'dirName' => 'banana pie incrediants', 'children' => NULL, ), ), ), ), ), 2 => array ( 'dirName' => 'cranberry', 'children' => array ( 0 => array ( 'dirName' => 'cranberry desert', 'children' => array ( 0 => array ( 'dirName' => 'cranberry desert incrediants', 'children' => NULL, ), ), ), 1 => array ( 'dirName' => 'cranberry juice', 'children' => array ( 0 => array ( 'dirName' => 'cranberry juice incrediants', 'children' => NULL, ), ), ), 2 => array ( 'dirName' => 'cranberry pie', 'children' => array ( 0 => 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?
|

12-22-2010, 08:08 AM
|
|
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--; } } }
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
| 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
HTML code is Off
|
|
|
|
|
|
| Login: |
|
|
| Advertisement: |
|
|
| Web Hosting News: |
|
|
|