0plus1 Blog

print_r output beautifier

by admin on May.13, 2009, under Programming

Today I wrote a script that needed to manipulate several multidimensional arrays, some of them pretty deep.

The standard php function print_r is an hellish mess, browsing through the marvellous php.net documentation I’ve found several examples to beautify the output of print_r (just to give credit: “Bob” and “Everett” were my inspiration) since no one actually did it the way I wanted I made a custom one myself:

function print_rr($array){
    foreach($array as $key=>$value){
       if(is_array($value)){
          $id = md5(rand());
          echo '[<a href="#" onclick="return expandParent(\''.$id.'\')">'.$key.'</a>]<br />';
          echo '<div id="'.$id.'" style="display:none;margin:10px;border-left:1px solid; padding-left:5px;">';
          print_rr($value, $count);
          echo '</div>';
       } else {
       echo "<b>$key</b>: ".$value."<br />";
       }
    }
echo '<script language="Javascript">
function expandParent(id){toggle="block";if(document.getElementById(id).style.display=="block"){toggle="none"}document.getElementById(id).style.display=toggle};
</script>';
}

Feel free to use it anyway you like it.

:, ,

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Archives

All entries, chronologically...