note 57618 added to ref.array
| From: | NickBullock-SafireX at osu1 dot php dot net | Date: | Sun, 09 Oct 2005 01:57:21 +0000 |
| Subject: | note 57618 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-96704@lists.php.net to get a copy of this message | ||
This is a function to easily output an array as an HTML table. Useful for debuging, displaying MySQL
output and an alternative to print_r().
<?
function array2table($arr,$width)
{
$count = count($arr);
if($count > 0){
reset($arr);
$num = count(current($arr));
echo "<table align=\"center\" border=\"1\"cellpadding=\"5\"
cellspacing=\"0\" width=\"$width\">\n";
echo "<tr>\n";
foreach(current($arr) as $key => $value){
echo "<th>";
echo $key." ";
echo "</th>\n";
}
echo "</tr>\n";
while ($curr_row = current($arr)) {
echo "<tr>\n";
$col = 1;
while ($curr_field = current($curr_row)) {
echo "<td>";
echo $curr_field." ";
echo "</td>\n";
next($curr_row);
$col++;
}
while($col <= $num){
echo "<td> </td>\n";
$col++;
}
echo "</tr>\n";
next($arr);
}
echo "</table>\n";
}
}
?>
<?
$test = array( 0 => array( "a" => 6, "b" => 25, "c" => 14
),
1 => array( "a" => 42, "b" => 75, "c" => 172 ),
2 => array( "a" => 12, "b" => 85, "c" => 17 ),
3 => array( "a" => 85, "b" => 74, "c" => 41 ),
4 => array( "a" => 63, "b" => 82, "c" => 19 ),
5 => array( "a" => 91, "b" => 788, "c" => 787 ) );
array2table($test,400); // Will output a table of 400px width
?>
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57618
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57618&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57618&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57618&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57618&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57618&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57618&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57618&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57618&report=yes
Search -- http://master.php.net/manage/user-notes.php