note 45134 added to ref.array

From: Date: Fri, 27 Aug 2004 02:29:12 +0000
Subject: note 45134 added to ref.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-75396@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/ref.array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+45134 Delete -- http://master.php.net/manage/user-notes.php?action=delete+45134&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+45134&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#75396) next »