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?
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.
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.
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.
The print_r() function prints the information about a variable in a more human-readable way.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With