Apr 15

Not for the first time I've searched for a way to visualize a multidimensional array in a tree view in my web browser without directly using echo or print. I wasn't able to find a concrete solution for this previously. However, this time I was put on the right track by a post of Kevin van Zonneveld. I've given it my own (quick and dirty as can be seen from the code) twist to get it to work. By just calling:

print implode(ArrayFunctions::toString($array));

The array is shown as a tree structure on the screen.
The implementation is as follows:

<?php
class ArrayFunctions {
  public static function toString($array){
    $result = array();
    $depth = 0;
    foreach($array as $k => $v) {
            $show_val = ( is_array($v) ? "" : $v );
 
            // show the indents
            $result []= str_repeat("  ", $depth);
            if($depth == 0) {
                // this is a root node. no parents
                $result []= "O ";
            } elseif(is_array($v)) {
                // this is a normal node. parents and children
                $result []= "+ ";
            } else {
                // this is a leaf node. no children
                $result []= "- ";
            }
 
            // show the actual node
            if ($show_val == "") {
                $result []= "<strong>{$k}</strong>:
";
            }
            else {
                $result []= $k . " (".$show_val.")"."
";
            }
 
            if(is_array($v)) {
                // this is what makes it recursive, rerun for childs
                $temp = self::toTree($v, ($depth+1));
                foreach($temp as $t) {
                    $result []= $t;
                }
            }
        }
        return implode($result);
    }
 
    private static function showtype($show_val) {
        // convert bools to text and quote 'text bools'!
        if (is_string($show_val) &&
           ($show_val == "true" || $show_val == "false")) {
            return "\"{$show_val}\"";
        }
        elseif (is_bool($show_val) && $show_val === true) {
            return "true";
        }
        elseif (is_bool($show_val) && $show_val === false) {
            return "false";
        }
        elseif (is_null($show_val)) {
            return "null";
        }
        else {
            return $show_val;
        }
    }
 
    private static function toTree($pieces, $depth = 0) {
        foreach($pieces as $k => $v) {
            // skip the baseval thingy. Not a real node.
            //if($k == "__base_val") continue;
            // determine the real value of this node.
            $show_val = ( is_array($v) ? "" : $v );
 
            $show_val = self::showtype($show_val);
 
            // show the indents
            $result []= str_repeat("  ", $depth);
            if($depth == 0) {
                // this is a root node. no parents
                $result []= "O ";
            } elseif(is_array($v)) {
                // this is a normal node. parents and children
                $result []= "+ ";
            } else {
                // this is a leaf node. no children
                $result []= "- ";
            }
 
            // show the actual node
            if ($show_val == "") {
                $result []= "<strong>{$k}</strong>:
";
            }
            else {
                $result []= $k . ": <i>{$show_val}</i>
";
            }
 
            if(is_array($v)) {
                // this is what makes it recursive, rerun for childs
                $temp = self::toTree($v, ($depth+1));
                if (is_array($temp)) {
                    foreach($temp as $t) {
                        $result []= $t;
                    }
                }
                else {
                    $result []= $t;
                }
            }
        }
        return $result;
    }
}
?>

One Response to “PHP multidimensional array to plain text tree structure”

  1. Shirly says:

    good stuff, thank you for posting.

Leave a Reply


+ four = 7

preload preload preload