note 105349 deleted from function.print-r by danbrown
| From: | danbrown@php.net | Date: | Wed, 10 Aug 2011 23:38:57 +0000 |
| Subject: | note 105349 deleted from function.print-r by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-182413@lists.php.net to get a copy of this message | ||
Note Submitter: jwood at JPL dot gov
----
<?php
// Displays a multi-dimensional array as a HTML List (Tree structure).
function displayTree($var) {
$newline = "\n";
foreach($var as $key => $value) {
if (is_array($value) || is_object($value)) {
$value = $newline . "<ul>" . displayTree($value) .
"</ul>";
}
if (is_array($var)) {
$i1 = stripos($value, "<li class=");
if ($i1 == false) {
$output .= "<li class=\"file\">" . $value .
"</li>" . $newline;
}
else {
$output .= $value . $newline;
}
}
else { // is_object
$output .= "<li class=\"folder\">" . $key . $value .
"</li>" . $newline;
}
}
return $output;
}
?>
///////////////////
Also need to add this to your style sheet (and get the necessary images...):
li.folder
{
list-style-image: url(../img/folder.gif);
}
li.file
{
list-style-image: url(../img/file.gif);
}
///////////////////
Example Usage:
<?php
...
echo "<ul>";
echo displayTree($array);
echo "</ul>";
?>