Function to make a visual tree like on windows explorer from an array using PHP

rfila used Ask the Experts™

I have a multi-dimensional array of names which I want to display on screen as a windows-explorer style tree.  I set out to make it myself but am having trouble getting there as it is slightly more involved than I expeted !

Is there a script anyone knows of which can do this for me ?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

So, this is something that you would click branches to toggle visible/hidden, yes?  Or is it just a static display of the tree?

$array = make_array_of_type_described_in_question();

// Method 1:

//Method 2:

//print_r, and var_dump are similar. They format the output differently.

//Method 3

// A custom recusive function to do the job:
// I can't think of how explorer does it at the minute,
// but you should be able to adapt the following.
function show_array($array, $indent = 0, $indent_diff = 2){
                foreach($array as $element){
                        show_array($element, $indent + $indent_diff, $indent_diff);
                for($i = 0; $i < $indent; ++$i){
                        echo ' ';
                echo $array . "\n";

$indent_diff is just a value by how much difference there is between various levels of indenting.

I did it using PHP and Javascript, the PHP handels requesting data ofm rthe DB and builds the javascript, the javascript then expands and colapses the tree as needed.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial