Skip to content

Instantly share code, notes, and snippets.

@paulbarbu
Created April 16, 2011 14:34
Show Gist options
  • Save paulbarbu/923151 to your computer and use it in GitHub Desktop.
Save paulbarbu/923151 to your computer and use it in GitHub Desktop.
Afisarea unei structuri de date recursive
<?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