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

时间:2017-02-03 09:52:37

标签: php html multidimensional-array

我有一个多维数组,如下所示:

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

我的以下代码无法正常使用

有人知道我如何将多维数组格式化为HTML表格吗?

<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>

我需要的输出如下: enter image description here

4 个答案:

答案 0 :(得分:2)

试试这个:在给定年份中每个月加上$total_for_year然后显示。

<?php 

function getsum($arr){
    $sum=0;
    foreach($arr as $k=>$v){
        $sum=$sum+$v['mtd'];
    }
    return $sum;
}
?>



 <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;$cnt=0;
            $size=sizeof($val);$total_for_year=getsum($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
                $cnt++;
                ?>
                <tr>

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

        }?>
        </tbody>
    </table>

DEMO

答案 1 :(得分:1)

您需要第一个周期来计算总数,因为您想在第一行输出它。它类似于:

<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>

$ count用于设置rowspan。

答案 2 :(得分:0)

试试这个:

<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;
            $total_row=0;
        ?>

            <?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
                $total_for_year=$total_for_year+$value['mtd'];
                $total_row++;
                ?>
                <tr>

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


            <?php } ?>
             <td rowspan="<?php echo $total_row;?>"><?php echo $total_for_year; ?></td>
            </tr>
        <?php } ?>

        </tbody>
    </table>

答案 3 :(得分:0)

<?php
$months = ["January","February", "March","April", "May","June","July","August", "September", "October", "November","December"];
?>
<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
$html_code = "";
foreach ($finalData as $year => $date_set) {
    $toal_revenue_in_year = 0;
    $i=0;
    foreach ($date_set as $date => $revenue) {
        $html_code .="<tr>";
        $month = $months[(int)explode("-", $date)[1]-1];
        $revenue_amount = $revenue["mtd"];
        $toal_revenue_in_year += $revenue_amount;
        $html_code .= "<td style=\"white-space: nowrap\">$month - $year</td>";
        $html_code .= "<td>".round($revenue_amount, 2)."</td>";
        if ($i==1) {
            $html_code .= "<td> $toal_revenue_in_year </td>";
        }
        $html_code .= "</tr>";
        $i++;
    }
}
echo $html_code;
?>
</tbody>
</table>

我只是不喜欢编写长代码。即使编写短代码需要很长时间。