note 105351 added to function.print-r

From: Date: Wed, 10 Aug 2011 23:04:10 +0000
Subject: note 105351 added to function.print-r
Groups: php.notes 
Request: Send a blank email to php-notes+get-182412@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)) { if (!stripos($value, "<li class=")) { $output .= "<li class=\"file\">" . $value . "</li>" . $newline; } else { $output .= $value . $newline; } } else { // is_object if (!stripos($value, "<li class=")) { $value = "<ul><li class=\"file\">" . $value . "</li></ul>" . $newline; } $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/105351 Del: integrated -- https://master.php.net/note/delete/105351/integrated Del: useless -- https://master.php.net/note/delete/105351/useless Del: bad code -- https://master.php.net/note/delete/105351/bad+code Del: spam -- https://master.php.net/note/delete/105351/spam Del: non-english -- https://master.php.net/note/delete/105351/non-english Del: in docs -- https://master.php.net/note/delete/105351/in+docs Del: other reasons-- https://master.php.net/note/delete/105351 Reject -- https://master.php.net/note/reject/105351 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#182412) next »