note 75506 deleted from function.in-array by felipe

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
Note Submitter: ashw1 - at - no spam - post - dot - cz 

----

If you ever need to print out an table of 2-dimensional array, here is one I made:

<?php
function MakeTableOf2DArray($db)
	{
	$col_cnt=0; $row_cnt=0;
	$col = array(); $row = array();

	foreach ($db as $key => $val)
		{
		if (! in_array($key,$col,true))
		    {
			$col[$col_cnt] = $key;
			$col_cnt++;
			}
		foreach ($val as $skey => $sval)
			{
			if (! in_array($skey,$row,true))
			    {
				$row[$row_cnt] = $skey;
				$row_cnt++;
				}
			}
		}

	$res = '<table><tr><td width="100">'.$col[$i].'</td>';
	for ($i=0;$i<$col_cnt;$i++)
		{
		$res .= '<td width="100">'.$col[$i].'</td>';
		}
	$res .= '</tr>'."\n";

	for ($i=0;$i<$row_cnt;$i++)
		{
		$res .= '<tr>';
		$res .= '<td>'.$row[$i].'</td>';
		for ($o=0;$o<$col_cnt;$o++)
			{
			$res .= '<td>'. (isset($db[$col[$o]][$row[$i]])? $db[$col[$o]][$row[$i]]: '&nbsp;').'</td>';
			}
		$res .= '</tr>'."\n";
		}

	$res .= '</table>';
	return $res;
	}

//example

$db = Array
(
    'ab' => Array
        (
            'janik' => 1 ,
            'vendula' => 3 ,
            'eva' => 5
        ) ,

    'lg' => Array
        (
            'janik' => 4 ,
            'eva' => 2 ,
            'misa' => 6
        )

);

echo MakeTableOf2DArray($db);

?>

prints out:

|         | ab | lg |
| janik   | 1  | 4  |
| vendula | 3  |    |
| eva     | 5  | 2  |
| misa    |    | 6  |
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.