已于2012-01-16更新:对于元素为数组的,也以表格输出(否则将数组子元素显示为array)
直接上代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>以表格形式输出二维数组</title> </head> <body> <?php $comment[100]=array('id'=>100,'parent'=>0,'content'=>'AAAAAAAAAA'); $comment[101]=array('id'=>101,'parent'=>100,'content'=>'BBBBBBBBB'); $comment[102]=array('id'=>102,'parent'=>100,'content'=>'CCCCCCCCCC'); $comment[103]=array('id'=>103,'parent'=>0,'content'=>'DDDDDDDDDDd'); $comment[104]=array('id'=>104,'parent'=>102,'content'=>'EEEEEEEE'); $comment[105]=array('id'=>105,'parent'=>101,'content'=>'FFFFFFFFF'); $comment[106]=array('id'=>106,'parent'=>0,'content'=>'GGGGGGGG'); $comment[107]=array('id'=>107,'parent'=>0,'content'=>'HHHHHHHHH'); $comment[108]=array('id'=>108,'parent'=>101,'content'=>'IIIIIIIIIIII'); $comment[109]=array('id'=>109,'parent'=>105,'content'=>'JJJJJJJJJJ'); $comment[110]=array('id'=>110,'parent'=>103,'content'=>'KKKKKKKKKK'); $comment[111]=array('id'=>111,'parent'=>108,'content'=>'LLLLLLLLL'); $comment[112]=array('id'=>112,'parent'=>105,'content'=>'MMMMMMMMMM'); $data=$comment; array_table_view($data); function array_table_view($data) { ?> <table width="95%" border="1" cellspacing="0" cellpadding="4"><?php if(!sizeof($data)){ ?> <tr> <td>empty array.</td> </tr><?php }else{ foreach($data as $item){ ?> <tr><?php foreach($item as $key => $val){ ?> <th><?php echo $key; ?></th><?php } ?> </tr> <?php break; } foreach($data as $item){ ?> <tr><?php foreach($item as $key => $val){ ?> <td><?php if(is_array($val)){ array_table_view($val); }else{ echo $val; } ?></td><?php } ?> </tr> <?php } } ?> </table> <?php } ?> </body> </html>
代码下载 array_table_view
代码下载 (请“复制-粘贴-保存”以上代码)
评论关闭。