note 75506 added to function.in-array

From: Date: Sat, 02 Jun 2007 20:54:05 +0000
Subject: note 75506 added to function.in-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-127129@lists.php.net to get a copy of this message
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 | ---- Server IP: 212.24.129.51 Probable Submitter: 85.135.10.2 ---- Manual Page -- http://www.php.net/manual/en/function.in-array.php Edit -- https://master.php.net/note/edit/75506 Del: integrated -- https://master.php.net/note/delete/75506/integrated Del: useless -- https://master.php.net/note/delete/75506/useless Del: bad code -- https://master.php.net/note/delete/75506/bad+code Del: spam -- https://master.php.net/note/delete/75506/spam Del: non-english -- https://master.php.net/note/delete/75506/non-english Del: in docs -- https://master.php.net/note/delete/75506/in+docs Del: other reasons-- https://master.php.net/note/delete/75506 Reject -- https://master.php.net/note/reject/75506 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#127129) next »