note 63802 deleted from ref.array by felipe
| From: | felipe@php.net | Date: | Sun, 30 Dec 2007 19:24:29 +0000 |
| Subject: | note 63802 deleted from ref.array by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134507@lists.php.net to get a copy of this message | ||
Note Submitter: administrador(ensaimada)sphoera(punt)com
----
With this simple function you can convert a (partial) bidimensional array into a XHTML table
structure:
<?php
$table = array();
$table[1][1] = "first";
$table[2][1] = "second";
$table[5][3] = "odd one";
$table[1][2] = "third";
echo matrix2table($table);
function matrix2table($arr,$tbattrs = "width='100%' border='1'",
$clattrs="align='center'"){
$maxX = $maxY = 1;
for ($x=0;$x<100;$x++){
for ($y=0;$y<100;$y++){
if ($arr[$x][$y]!=""){
if ($maxX < $x) $maxX = $x;
if ($maxY < $y) $maxY = $y;
}
}
}
$retval = "<table $tbattrs>\n";
for ($x=1;$x<=$maxX;$x++){
$retval.=" <tr>\n";
for ($y=1;$y<=$maxY;$y++){
$retval.= (isset($arr[$x][$y]))
?" <td $clattrs>".$arr[$x][$y]."</td>\n"
:" <td $clattrs> </td>\n";
}
$retval.=" </tr>\n";
}
return $retval."</table>\n";
}
?>
more scripts at http://www.sphoera.com