dongzi0857 2017-02-03 09:52
浏览 356
已采纳

如何将多维数组数据显示到HTML表

I have a multidimensional array like below:

$finalData = [
    '2017' => [
        '2017-02' => [
            'mtd' => 317
        ],
        '2017-01' => [
            'mtd' => 1012
        ]
    ],
    '2016' => [
        '2016-12' => [
            'mtd' => 1125.01
        ],
        '2016-11' => [
            'mtd' => 355
        ]
    ]
];

my below code is not working properly

Does anybody know how I format a Multidimensional Array into an HTML Table?

<table id="responsive-datatables"
           class="table table-bordered table-striped table-hover dt-responsive non-responsive" cellspacing="0"
           width="100%">
        <thead>
        <tr>
            <th style="white-space: nowrap">Month - Year</th>
            <th>MTD</th>
            <th>YTD</th>
        </tr>
        </thead>
        <tbody>
        <?php foreach ($finalData as $year => $val) { ?>
            <?php foreach ($val as $month => $value) {
                if ($month === 'ytd') {
                    continue;
                }
                $month_year = explode('-', $month);
                switch ($month_year[1]) {
                    case '01':
                        $month = 'January';
                        break;
                    case '02':
                        $month = 'February';
                        break;
                    case '03':
                        $month = 'March';
                        break;
                    case "04":
                        $month = 'April';
                        break;
                    case '05':
                        $month = 'May';
                        break;
                    case "06":
                        $month = 'June';
                        break;
                    case '07':
                        $month = 'July';
                        break;
                    case '08':
                        $month = 'August';
                        break;
                    case '09':
                        $month = 'September';
                        break;
                    case '10':
                        $month = 'October';
                        break;
                    case '11':
                        $month = 'November';
                        break;
                    case '12':
                        $month = 'December';
                        break;
                    default:
                        $month = 'Not  Matched';
                        break;
                }//end switch
                ?>


                <td style="white-space: nowrap"><?php echo $month . " - " . $month_year[0]; ?> </td>
                <td><?php echo round(@$value['mtd'], 2); ?></td>
                <td><?php echo $total_for_year; ?></td>

            <?php } ?>
            </tr>
        <?php } ?>

        </tbody>
    </table>

My required output like below: enter image description here

  • 写回答

4条回答 默认 最新

  • dtvnbe1428 2017-02-03 10:24
    关注

    You need a first cycle to calculate the total, because you want to output it in the first row. It's something like:

    <table id="responsive-datatables"
       class="table table-bordered table-striped table-hover dt-responsive non-responsive" cellspacing="0"
       width="100%">
    <thead>
    <tr>
        <th style="white-space: nowrap">Month - Year</th>
        <th>MTD</th>
        <th>YTD</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($finalData as $year => $val) {$total_for_year = 0; $count = 0;
        foreach ($val as $month => $value) {$count++;$total_for_year +=$value['mtd'];}
        $first = true;
        foreach ($val as $month => $value) {
            if ($month === 'ytd') {
                continue;
            }
            $month_year = explode('-', $month);
            switch ($month_year[1]) {
                case '01':
                    $month = 'January';
                    break;
                case '02':
                    $month = 'February';
                    break;
                case '03':
                    $month = 'March';
                    break;
                case "04":
                    $month = 'April';
                    break;
                case '05':
                    $month = 'May';
                    break;
                case "06":
                    $month = 'June';
                    break;
                case '07':
                    $month = 'July';
                    break;
                case '08':
                    $month = 'August';
                    break;
                case '09':
                    $month = 'September';
                    break;
                case '10':
                    $month = 'October';
                    break;
                case '11':
                    $month = 'November';
                    break;
                case '12':
                    $month = 'December';
                    break;
                default:
                    $month = 'Not  Matched';
                    break;
            }//end switch
            ?>
    
          <tr>
            <td style="white-space: nowrap"><?php echo $month . " - " . $month_year[0]; ?> </td>
            <td><?php echo round(@$value['mtd'], 2); ?></td>
            <?php if($first) {echo '<td rowspan="'.$count.'">'.$total_for_year.'</td>'; $first = false;} ?>
        </tr>
        <?php } ?>
    <?php } ?>
    
    </tbody>
    

    The $count is used to set the rowspan.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题