15

I'm looking to be able to produce a nicely formatted table with rows and columns from the contents of a print_r array statement?

Any ideas?

2
  • 1
    What do you define as "nicely formatted"? Do you want an HTML table? Some XML? JSON? CVS? Human-readable plaintext? Commented Sep 6, 2009 at 18:29
  • Instead of using print_r just use a foreach on the array, as jasondavis suggests, below. Also, in line with strager's comment, I'd echo the request for a specific request/question. It would help, at the least, to know what's in the array, is it one, two, three dimensional..? Commented Sep 6, 2009 at 19:30

4 Answers 4

78

Here is a very simple way to print pretty arrays with html pre tag:

<?php
$myarray = array('a','b','c');
echo '<pre>';
print_r($myarray);
echo '</pre>';
?>
Sign up to request clarification or add additional context in comments.

3 Comments

This solution is simple yet effective. Just what I needed!
Or this one-liner: echo "<pre>" . print_r($myarray, true) . "</pre>";
This is fantastic
21

Your question is a bit vague, but did you mean something like this:

https://github.com/ospinto/dBug

dbug bar

1 Comment

The download URL on that page is badly formed - correct link is dbug.ospinto.com/dl/dBug.zip .
14

Try this out, could be improved but it works.

function myprint_r($my_array) {
    if (is_array($my_array)) {
        echo "<table border=1 cellspacing=0 cellpadding=3 width=100%>";
        echo '<tr><td colspan=2 style="background-color:#333333;"><strong><font color=white>ARRAY</font></strong></td></tr>';
        foreach ($my_array as $k => $v) {
                echo '<tr><td valign="top" style="width:40px;background-color:#F0F0F0;">';
                echo '<strong>' . $k . "</strong></td><td>";
                myprint_r($v);
                echo "</td></tr>";
        }
        echo "</table>";
        return;
    }
    echo $my_array;
}

2 Comments

How can you adapt this function so it returns a final built up string instead of printing the table out?
@user3574492: Drop the echo lines and store it into a variable instead with concatenation. Then have function return the variable.
-1

Here is another nice example that I found. Same output, longer code, little bit more color.

function print_nice($elem,$max_level=10,$print_nice_stack=array()){
if(is_array($elem) || is_object($elem)){
    if(in_array(&$elem,$print_nice_stack,true)){
        echo "<font color=red>RECURSION</font>";
        return;
    }
    $print_nice_stack[]=&$elem;
    if($max_level<1){
        echo "<font color=red>nivel maximo alcanzado</font>";
        return;
    }
    $max_level--;
    echo "<table border=1 cellspacing=0 cellpadding=3 >";
    if(is_array($elem)){
        echo '<tr><td colspan=2  style="background-color:#333333;"><strong><font  color=white>ARRAY</font></strong></td></tr>';
    }else{
        echo '<tr><td colspan=2 style="background-color:#333333;"><strong>';
        echo '<font color=white>OBJECT Type: '.get_class($elem).'</font></strong></td></tr>';
    }
    $color=0;
    foreach($elem as $k => $v){
        if($max_level%2){
            $rgb=($color++%2)?"#888888":"#BBBBBB";
        }else{
            $rgb=($color++%2)?"#8888BB":"#BBBBFF";
        }
        echo '<tr><td valign="top" style="width:40px;background-color:'.$rgb.';">';
        echo '<strong>'.$k."</strong></td><td>";
        print_nice($v,$max_level,$print_nice_stack);
        echo "</td></tr>";
    }
    echo "</table>";
    return;
}
if($elem === null){
    echo "<font color=green>NULL</font>";
}elseif($elem === 0){
    echo "0";
}elseif($elem === true){
    echo "<font color=green>TRUE</font>";
}elseif($elem === false){
    echo "<font color=green>FALSE</font>";
}elseif($elem === ""){
    echo "<font color=green>EMPTY STRING</font>";
}else{
    echo str_replace("\n","<strong><font color=red>*</font></strong><br>\n",$elem);
}

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.