0

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

2
  • Have you calculate $total_for_year? Commented Feb 3, 2017 at 10:01
  • The total count is not a problem. I can not format output. please see output screen for your better understanding. Commented Feb 3, 2017 at 10:11

4 Answers 4

2

Try this: sum $total_for_year for every month in a given year then display.

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

Sign up to request clarification or add additional context in comments.

5 Comments

the total count is okay. But HTML table output is not working as proper formating. I want to show every year total using row span.
@Faisal have you tried using rowspan in the last td.
did you check the output?
@Faisal what is the issue.
@Faisal i have attached a link try that.
1

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.

1 Comment

it was missing and badly placed the tr's, try again.
0

try this:

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

1 Comment

its not working. Output goes to 2016. please check this screenshot. awesomescreenshot.com/image/2131287/…
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>

I just don't like to write long codes..even if it takes long to write short code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.