note 45134 deleted from ref.array by aidan
| From: | aidan@php.net | Date: | Fri, 27 Aug 2004 03:50:30 +0000 |
| Subject: | note 45134 deleted from ref.array by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-75399@lists.php.net to get a copy of this message | ||
Note Submitter: benjamin at moonfactory dot co dot jp
----
This is a function that converts an associative array to an HTML table.
I find it usefull for debugging.
<?php
function array_to_html($array, $p = 0)
{
$colors = array('#EEEEEE','#CCCCFF');
$str = "<table";
$str .= " style='{border-style: none none none none ;";
$str .= " border-width: 1;}'";
$str .= " cellspacing = '0' cellpadding = '5'
width='100%'>\n";
while(list($key,$value) = each($array) )
{
$str .= "<tr bgcolor='$colors[$p]'>\n";
$str .= "<td valign='top' ";
$str .= " style='{border-style: solid none none solid ; ";
$str .= " border-width: 1; border-color: #555555}";
$str .= " '>$key</td>\n";
$str .= "<td valign='top' ";
$str .= " style='{border-style: solid none none solid ;";
$str .= " border-width: 1; border-color: #555555}'>\n";
if (is_array($value))
{
$str .= array_to_html($value, $p);
}else{
$str .= $value;
}
$str .= "</td></tr>";
$p = !$p;
}
$str .= "</table>";
return $str;
}
Use it like this:
<?php
$foo = array(
"vegetable" => array(
"carot",
"tomato",
"apple"=>array("green","red","blue")
),
"fruits" => array("orange","banana"),
"others");
echo array_to_html($foo);
?>
Ben