Php 如何向HTML表显示多维数组数据
我有一个多维数组,如下所示:Php 如何向HTML表显示多维数组数据,php,html,multidimensional-array,Php,Html,Multidimensional Array,我有一个多维数组,如下所示: $finalData = [ '2017' => [ '2017-02' => [ 'mtd' => 317 ], '2017-01' => [ 'mtd' => 1012 ] ], '2016' => [ '2016-12' => [ 'mtd' =
$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>
月-年
MTD
年初至今
我需要的输出如下所示:
试试这个:给定年份中每个月的sum
$total\u for\u 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>
月-年
MTD
年初至今
尝试以下操作:为给定年份中的每个月计算一年的总和$total\u,然后显示
<?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>
月-年
MTD
年初至今
试试这个:
<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>
月-年
MTD
年初至今
试试这个:
<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>
月-年
MTD
年初至今
您需要第一个循环来计算总数,因为您希望在第一行中输出它。有点像:
<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>
月-年
MTD
年初至今
$count用于设置行跨度。您需要第一个周期来计算总数,因为您希望在第一行中输出它。有点像:
<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>
<?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>
月-年
MTD
年初至今
$count用于设置行跨度。
<?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>
月-年
MTD
年初至今
我只是不喜欢写长代码。即使写短代码需要很长时间。
月-年
MTD
年初至今
我只是不喜欢写长代码。即使写短代码需要很长的时间。你计算过一年的$total吗?总数不是问题。我无法格式化输出。请查看输出屏幕以便更好地理解。您是否计算了年的$total_?总计数不是问题。我无法格式化输出。请参阅输出屏幕,以便更好地理解。总计数正常。但HTML表输出的格式不正确。我想用行span显示每年的总数。@Faisal您在上一个td中是否尝试过使用行span。您检查过输出吗?@Faisal有什么问题。@Faisal我已附加了一个链接,请尝试一下。总数可以。但HTML表输出的格式不正确。我想用行span显示每年的总数。@Faisal您在上一个td中是否尝试过使用行span。是否检查了输出?@Faisal有什么问题。@Faisal我附加了一个链接,请尝试一下。它不起作用。产量将持续到2016年。请检查这个屏幕截图。它不起作用。产量将持续到2016年。请检查这个屏幕截图。tr丢失且位置不正确,请重试。tr丢失且位置不正确,请重试。