note 55738 added to function.print-r
| From: | warhog at warhog dot net | Date: | Fri, 12 Aug 2005 00:01:13 +0000 |
| Subject: | note 55738 added to function.print-r | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93470@lists.php.net to get a copy of this message | ||
For very long arrays I have written a little function which formats an array quite nice and uses
javascript for browsing it like a tree. The function is very customizable with the $style parameter.
For me it's of great use for browsing large array's, for example when those are used in
language-files in some script and so on. It may even be used in "real" scripts for the
"real" front-end, cause the tree can very easily be styled (look at the function or the
outputted source and you'll see what i mean).
Here's the function:
<?php
function print_r_html($arr, $style = "display: none; margin-left: 10px;")
{ static $i = 0; $i++;
echo "\n<div id=\"array_tree_$i\" class=\"array_tree\">\n";
foreach($arr as $key => $val)
{ switch (gettype($val))
{ case "array":
echo "<a onclick=\"document.getElementById('";
echo array_tree_element_$i."').style.display = ";
echo "document.getElementById('array_tree_element_$i";
echo "').style.display == 'block' ?";
echo "'none' : 'block';\"\n";
echo "name=\"array_tree_link_$i\"
href=\"#array_tree_link_$i\">".htmlspecialchars($key)."</a><br
/>\n";
echo "<div class=\"array_tree_element_\"
id=\"array_tree_element_$i\" style=\"$style\">";
echo print_r_html($val);
echo "</div>";
break;
case "integer":
echo "<b>".htmlspecialchars($key)."</b> =>
<i>".htmlspecialchars($val)."</i><br />";
break;
case "double":
echo "<b>".htmlspecialchars($key)."</b> =>
<i>".htmlspecialchars($val)."</i><br />";
break;
case "boolean":
echo "<b>".htmlspecialchars($key)."</b> => ";
if ($val)
{ echo "true"; }
else
{ echo "false"; }
echo "<br />\n";
break;
case "string":
echo "<b>".htmlspecialchars($key)."</b> =>
<code>".htmlspecialchars($val)."</code><br />";
break;
default:
echo "<b>".htmlspecialchars($key)."</b> =>
".gettype($val)."<br />";
break; }
echo "\n"; }
echo "</div>\n"; }
?>
The function as it is now does not support the $return parameter as print_r does and will create an
endless loop like print_r did in php-versions < 4.0.3 when there is an element which contains a
reference to a variable inside of the array to print out :-/
I've tested it with PHP 5.0.6 and PHP 4.2.3 - no problems except those already mentioned.
please e-mail me if you've got a solution for the problems i've mentioned, i myself are
not able to solve them 'cause i don't know how the hell i can find out whether a variable
is a reference or not.
----
Manual Page -- http://www.php.net/manual/en/function.print-r.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55738
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55738&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55738&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55738&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55738&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55738&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55738&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55738&report=yes
Search -- http://master.php.net/manage/user-notes.php