note 90759 added to function.print-r
| From: | Bob at osu1 dot php dot net | Date: | Fri, 08 May 2009 10:19:02 +0000 |
| Subject: | note 90759 added to function.print-r | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-154349@lists.php.net to get a copy of this message | ||
Here is a function that formats the output of print_r as a expandable/collapsable tree list using
HTML and JavaScript.
<?php
function print_r_tree($data)
{
// capture the output of print_r
$out = print_r($data, true);
// replace something like '[element] => <newline> (' with <a
href="javascript:toggleDisplay('...');">...</a><div
id="..." style="display: none;">
$out = preg_replace('/([ \t]*)(\[[^\]]+\][ \t]*\=\>[ \t]*[a-z0-9 \t_]+)\n[
\t]*\(/iUe',"'\\1<a href=\"javascript:toggleDisplay(\''.(\$id =
substr(md5(rand().'\\0'), 0, 7)).'\');\">\\2</a><div
id=\"'.\$id.'\" style=\"display: none;\">'", $out);
// replace ')' on its own on a new line (surrounded by whitespace is ok) with
'</div>
$out = preg_replace('/^\s*\)\s*$/m', '</div>', $out);
// print the javascript function toggleDisplay() and then the transformed output
echo '<script language="Javascript">function toggleDisplay(id) {
document.getElementById(id).style.display = (document.getElementById(id).style.display ==
"block") ? "none" : "block";
}</script>'."\n$out";
}
?>
Pass it a multidimensional array or object and each sub-array/object will be hidden and replaced by
a html link that will toggle its display on and off.
Its quick and dirty, but great for debugging the contents of large arrays and objects.
Note: You'll want to surround the output with <pre></pre>
----
Server IP: 117.104.162.229
Probable Submitter: 58.96.44.86 (proxied: 58.96.44.86)
----
Manual Page -- http://www.php.net/manual/en/function.print-r.php
Edit -- https://master.php.net/note/edit/90759
Del: integrated -- https://master.php.net/note/delete/90759/integrated
Del: useless -- https://master.php.net/note/delete/90759/useless
Del: bad code -- https://master.php.net/note/delete/90759/bad+code
Del: spam -- https://master.php.net/note/delete/90759/spam
Del: non-english -- https://master.php.net/note/delete/90759/non-english
Del: in docs -- https://master.php.net/note/delete/90759/in+docs
Del: other reasons-- https://master.php.net/note/delete/90759
Reject -- https://master.php.net/note/reject/90759
Search -- https://master.php.net/manage/user-notes.php