Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php print_r nice table

Tags:

php

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?

like image 783
Adam Chetnik Avatar asked Sep 06 '09 18:09

Adam Chetnik


People also ask

What is the difference between ECHO and Print_r in PHP?

The print and echo are both language constructs to display strings. The echo has a void return type, whereas print has a return value of 1 so it can be used in expressions. The print_r is used to display human-readable information about a variable.

How can we display information of a variable and readable by a human with PHP?

print_r() displays information about a variable in a way that's readable by humans. print_r(), var_dump() and var_export() will also show protected and private properties of objects.

Where do you use Print_r () statement in PHP?

What is the Use of print_r() Function in PHP? To inspect or check the structure and values of an array in a human-readable format on the screen, you can use the print r() or var dump() statements in PHP. var dump() usually provides more information than print_r in PHP.

What is use of Print_r () function?

The print_r() function prints the information about a variable in a more human-readable way.


1 Answers

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;
}
like image 135
JasonDavis Avatar answered Sep 24 '22 22:09

JasonDavis