青剑开源代码之PHP输出复杂数组为表格视图

时间:13-10-14 栏目:网站开发 作者:admin 评论:0 点击: 3,429 次

PHP输出复杂数组为表格函数

/**
* 将數組输出为表格 2013/11/23
* @param array $games 数组内容
* @param array $keys 数组标题,不指定则默认以数组KEY作为标题
* @param array $showTitle 是否显示标题
*/
function outTable($games, $nkeys=null,$showTitle=true,$sub=false) {

$out = '';
$error = false;
if($nkeys==null){
if(!is_array($games) || count($games)<1){
$error =true;
$out .= print_r($games,true);
}else
if(!array_key_exists(key($games),$games)){
$error =true;
$out .= print_r($games,true);
}else
if(is_array(current($games))){
$keys = array_keys(current($games));
}else{
$out .= '<table cellspacing="1" cellpadding="3" border="0" class="table0">';
$i = 0;
foreach ($games as $game_id => $game) {
$i++;
$out .= '<tr bgcolor="' . (($i % 2) ? "#EEEEEE" : '#FFFFFF') . '">';
$out .= '<th align="right">'.$game_id.'</th>';
$out .= '<td>';
if (is_array($game))
$out .= outTable($game,null,false,true);
else
$out .= $game;
$out .= '</td>';

$out .= '</tr>';
}
$out .= '</table>';
$error =true;
}
}else{
$keys = $nkeys;
}
if(!is_array($games)){
$error =true;
$out .=print_r($games,true);

}

if(!$error){
$out .= '<table cellspacing="1" cellpadding="3" border="0" class="table0">';
$out .= '<tr id=head style="">';
if($showTitle){
$out .= '<th>key</th>';
foreach ($keys as $k => $v) {
$out .= '<th>';
$out .= $v;
$out .= '</th>';
}
$out .= '</tr>';
} $i = 0;
foreach ($games as $game_id => $game) {
$i++;

$out .= '<tr bgcolor="' . (($i % 2) ? "#EEEEEE" : '#FFFFFF') . '">';
if($showTitle) $out .= '<th>'.$game_id.'</th>';

if(is_array($game)){
foreach ($keys as $k => $v) {
$out .= '<td>';
if (is_array($game[$v]))
$out .= outTable($game[$v],null,false,true);
else
$out .= $game[$v];
$out .= '</td>';
}
if($nkeys==null){
foreach ($game as $v => $vv) {
if(in_array($v,$keys))continue;
$out .= '<td>';
if (is_array($game[$v]))
$out .= outTable($game[$v],null,false,true);
else
$out .= $game[$v];
$out .= '</td>';
}

}

}else{
$out .= '<td colspan="'.count($keys).'">';
$out .= $game;
$out .= '</td>';
}

$out .= '</tr>';
}
$out .= '</table>';

}
if($sub==false)echo $out;
return $out;
}

使用示例PHP代码:

$data = array (
  'state' => '1',
  'data' => 
  array (
    'result' => 
    array (
      0 => 
      array (
        0 => 'Z201310148100062107',
        1 => '2013-10-14 22:26:28',
        2 => '2013101481',
        3 => '总和 尾小',
        4 => 1.985,
        5 => 'A',
        6 => '15',
        7 => '0.72%',
        8 => '14.88',
        9 => '成功',
        10 => '0',
        11 => 'klc',
      ),
      1 => 
      array (
        0 => 'Z201310148100062106',
        1 => '2013-10-14 22:26:28',
        2 => '2013101481',
        3 => '总和 尾大',
        4 => 1.985,
        5 => 'A',
        6 => '14',
        7 => '0.72%',
        8 => '13.89',
        9 => '成功',
        10 => '0',
        11 => 'klc',
      ),
      2 => 
      array (
        0 => 'Z201310148100062105',
        1 => '2013-10-14 22:26:28',
        2 => '2013101481',
        3 => '总和 双',
        4 => 1.985,
        5 => 'A',
        6 => '13',
        7 => '0.72%',
        8 => '12.89',
        9 => '成功',
        10 => '0',
        11 => 'klc',
      ),
      3 => 
      array (
        0 => 'Z201310148100062104',
        1 => '2013-10-14 22:26:28',
        2 => '2013101481',
        3 => '总和 单',
        4 => 1.985,
        5 => 'A',
        6 => '12',
        7 => '0.72%',
        8 => '11.90',
        9 => '成功',
        10 => '0',
        11 => 'klc',
      ),
      4 => 
      array (
        0 => 'Z201310148100062103',
        1 => '2013-10-14 22:26:28',
        2 => '2013101481',
        3 => '总和 小',
        4 => 1.985,
        5 => 'A',
        6 => '11',
        7 => '0.72%',
        8 => '10.91',
        9 => '成功',
        10 => '0',
        11 => 'klc',
      ),
      5 => 
      array (
        0 => 'Z201310148100062102',
        1 => '2013-10-14 22:26:28',
        2 => '2013101481',
        3 => '总和 大',
        4 => 1.985,
        5 => 'A',
        6 => '10',
        7 => '0.72%',
        8 => '9.92',
        9 => '成功',
        10 => '0',
        11 => 'klc',
      ),
    ),
    'alltotal' => 
    array (
      0 => '75',
      1 => '74',
    ),
    'st' => '2',
    'gameId' => 'allplay',
    'platform' => 'klc',
  ),
  'errors' => '',
);
outTable($data);

输出效果:

state 1
data
result
0 Z201310148100062107
1 2013-10-14 22:26:28
2 2013101481
3 总和 尾小
4 1.985
5 A
6 15
7 0.72%
8 14.88
9 成功
10 0
11 klc
0 Z201310148100062106
1 2013-10-14 22:26:28
2 2013101481
3 总和 尾大
4 1.985
5 A
6 14
7 0.72%
8 13.89
9 成功
10 0
11 klc
0 Z201310148100062105
1 2013-10-14 22:26:28
2 2013101481
3 总和 双
4 1.985
5 A
6 13
7 0.72%
8 12.89
9 成功
10 0
11 klc
0 Z201310148100062104
1 2013-10-14 22:26:28
2 2013101481
3 总和 单
4 1.985
5 A
6 12
7 0.72%
8 11.90
9 成功
10 0
11 klc
0 Z201310148100062103
1 2013-10-14 22:26:28
2 2013101481
3 总和 小
4 1.985
5 A
6 11
7 0.72%
8 10.91
9 成功
10 0
11 klc
0 Z201310148100062102
1 2013-10-14 22:26:28
2 2013101481
3 总和 大
4 1.985
5 A
6 10
7 0.72%
8 9.92
9 成功
10 0
11 klc
alltotal 75 74        
st 2
gameId allplay
platform klc
errors  

声明: 本文由( admin )原创编译,转载请保留链接: 青剑开源代码之PHP输出复杂数组为表格视图

青剑开源代码之PHP输出复杂数组为表格视图:等您坐沙发呢!

发表评论


------====== 本站公告 ======------
联系信息:
lxq73061#qq.com qq:583964941
支付宝:https://me.alipay.com/lxq73061
相关插件程序等信息均会在站内发布,敬请关注。

读者排行