How can I print the array in a Tree-like format--making it easier to read?
9 Answers
Try:
<pre><?php print_r($var); ?></pre>
It will give the proper tree structure that HTML's whitespace policy trims out.
1 Comment
<pre><?php print_r($var,true); ?></pre> gives more consistent (tree-like) results (though I haven't researched why that is)Are you wrapping the output in <pre> tags? That should get you pretty decent output, because it will show the spaces. Another option would be to install the xdebug extension, which then can replace var_dump so that it generates more-readable HTML output.
1 Comment
var_dump it provides supports arrays very nicely, almost eliminating the need for print_r entirely.function pr($var)
{
print '<pre>';
print_r(htmlspecialchars($var));
print '</pre>';
}
pr($myArray);
2 Comments
htmlspecialchars please. OK, so security isn't a concern for debugging code (though escaping certainly a habit you should always have), but any ‘<’ characters in your variables will mess up the output.function pr($var) { print '<pre>' . htmlspecialchars(print_r($var, true)) . '</pre>'; }I found it's a good idea to print_r as follows
printf("<pre>%s</pre>", print_r($array, true));
It may not be ideal, but it's easier to read.
1 Comment
Try taking a look at Zend_Debug, a relatively plug-and-play module from the Zend Framework which does an excellent job at effectively dumping complex variables.
Usage:
$my_var = new StdObject(); // or whatever
Zend_Debug::dump($my_var);
die; // optional, prevents routing, forwarding away, etc.
print_rprints in plain text and not HTML. So you need to look at the source code to see the original output.