【问题标题】:PHP 3 dimensional Array to HTML TABLEPHP 3维数组到HTML TABLE
【发布时间】:2013-10-26 07:58:29
【问题描述】:

如果我把它作为一个三维数组:

(int) Year 1 => array(
    'Department 1' => array(
        'Sales' => '12345',
        'Revenue' => '12345',
        'total' => '12345'
    ),
    'Department 2' => array(
        'Sales' => '12345',
        'Revenue' => '12345',
        'total' => '12345'
    ),
),
(int) Year 2 => array(
    'Department 1' => array(
        'Sales' => '12345',
        'Revenue' => '12345',
        'total' => '12345'
    ),
    'Department 2' => array(
        'Sales' => '12345',
        'Revenue' => '12345',
        'total' => '12345'
    ),

)

我将如何在 HTML 表格中显示它。以年为标题。各行开头左侧的部门。以及年列中单独列中的销售额、修订和总计。

输出应该是这样的

Department_______YEAR1_________Year2

Department 1  |Sales,Reveue |Sales,Reveue
Department 2  |Sales,Reveue |Sales,Reveue

【问题讨论】:

  • 请包括应该是什么输出
  • 指定包含year1和year2的主数组
  • 那么你有什么尝试?我们不是您的个人脚本军队。

标签: php html arrays loops html-table


【解决方案1】:

首先你必须在循环之外创建一个表格和TH

<table>
<tr>
<th>Department</th>
<th>Year 1</th>
<th>Year 2</th>
</tr>
<tr>
<?php

foreach ($1stvalue as $first => $2ndvalue ) {
    foreach ($2ndvalue as $second => $3rdvalue) {
        foreach ($3rdvalue as $third) {   
            echo "<td>".$first."</td>"."<td>".$second."</td>"."<td>".$third."</td>"."</tr>";
        }
    }
}
?>

你需要foreach循环每个维度并在你得到它时回显它

【讨论】:

  • 这将为数组中的每个值创建一个列。这不是 OP 想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-27
  • 2023-03-30
  • 2015-10-15
  • 2013-02-28
  • 2016-10-02
  • 2019-12-14
  • 2012-08-11
相关资源
最近更新 更多