note 105349 added to function.print-r
| From: | jwoodatJPLdotgov at osu1 dot php dot net | Date: | Wed, 10 Aug 2011 22:12:24 +0000 |
| Subject: | note 105349 added to function.print-r | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182410@lists.php.net to get a copy of this message | ||
<?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>";
?>
----
Server IP: 69.147.83.197
Probable Submitter: 137.78.80.195
----
Manual Page -- http://www.php.net/manual/en/function.print-r.php
Edit -- https://master.php.net/note/edit/105349
Del: integrated -- https://master.php.net/note/delete/105349/integrated
Del: useless -- https://master.php.net/note/delete/105349/useless
Del: bad code -- https://master.php.net/note/delete/105349/bad+code
Del: spam -- https://master.php.net/note/delete/105349/spam
Del: non-english -- https://master.php.net/note/delete/105349/non-english
Del: in docs -- https://master.php.net/note/delete/105349/in+docs
Del: other reasons-- https://master.php.net/note/delete/105349
Reject -- https://master.php.net/note/reject/105349
Search -- https://master.php.net/manage/user-notes.php