Created
April 16, 2011 14:34
-
-
Save paulbarbu/923151 to your computer and use it in GitHub Desktop.
Afisarea unei structuri de date recursive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_directory_structure($dirpath){ | |
$ar = array(); | |
$dh = opendir($dirpath); | |
while($entry = readdir($dh)){ | |
if($entry != "." && $entry != ".."){ | |
if(is_dir($dirpath.DIRECTORY_SEPARATOR.$entry)) { | |
$ar[$entry] = get_directory_structure($dirpath.DIRECTORY_SEPARATOR.$entry); | |
} | |
else{ | |
$ar[] = $entry; | |
} | |
} | |
} | |
closedir($dh); | |
return $ar; | |
} | |
function render_recursive_array_to_ul($array){ | |
$str = "<ul>"; | |
foreach($array as $k => $v){ | |
if(is_array($v)){ | |
$str .= "<li><b>$k</b>"; | |
$str .= render_recursive_array_to_ul($v); | |
$str .= "</li>"; | |
} | |
else{ | |
$str .= "<li>$v</li>"; | |
} | |
} | |
$str .= "</ul>"; | |
return $str; | |
} | |
$str = render_recursive_array_to_ul(get_directory_structure("./dir")); | |
echo $str; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment