While in development mode on my site, I often used to resort to print_r to view the contents of a given array. There were a couple set backs about this function that I did not like.
First, it prints everything on the page in a single lined string. The user needs to go to View Source to view the array structure easily.
Second, it prints it at the position in the script it was called. Since, I return my html into a variable and then print the varaible at the end of the script, it always displays the printed array at the top of the screen.
Therefore, I put together a very simple and straightforward function to display array contents. I was going to go post it over at php.net, but their user contributed notes do not seem to be functioning. So, I figured I would post it here for now and get feedback from you guys, till php.net is working.
Any thoughts on how to improve it?
[code=php]
<?php
function showarr($array){
$show=”<div style=’margin:5px; font:normal 10px Courier,Arial; text-align:left; ‘>==========<br />n”;
function sharr($key,$input,$num){
$px=$num*15;
$retarr=”<div style=’margin-left:{$px}px; ‘>[{$key}] => ‘{$input}'</div>n”;
if(is_array($input)){
$num++;
foreach($input AS $skey=>$sval){
$retarr.=sharr($skey,$sval,$num);
}
$num–;
}
return $retarr;
}
$num=0;
foreach($array AS $key=>$val){
$show.=sharr($key,$val,$num);
}
$show.=”==========</div>n”;
return $show;
}
?>