note 75506 deleted from function.in-array by felipe
| From: | felipe@php.net | Date: | Sun, 30 Dec 2007 19:01:14 +0000 |
| Subject: | note 75506 deleted from function.in-array by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134496@lists.php.net to get a copy of this message | ||
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]]:
' ').'</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 |