Voting

: min(eight, one)?
(Example: nine)

The Note You're Voting On

warhog at warhog dot net
19 years ago
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.

<< Back to user notes page

To Top