note 63802 deleted from ref.array by felipe

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
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>&nbsp;</td>\n";
		}
		$retval.=" </tr>\n";
	}	
	return $retval."</table>\n";
}

?>
more scripts at http://www.sphoera.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.