在 PHP 中使用关联数组创建多个 html 表

Create multiple html tables with associative array in PHP

我有一个数组数组,访问数据没问题,但是当我尝试基于一个键创建单独的 tables 时,该键设置为数据从数据库中提取的数字月份落在。

I have: 
array(1) {
  [5]=>
  array(12) {
    ["ID"]=>
    string(5) "1001"
    ["created_timestamp"]=>
    string(26) "2015-06-11 19:10:00"
    ["firstname"]=>
    string(4) "John"
    ["lastname"]=>
    string(3) "Doe"
  }
}
array(1) {
  [5]=>
  array(12) {
    ["ID"]=>
    string(5) "1002"
    ["created_timestamp"]=>
    string(26) "2015-06-12 19:10:00"
    ["firstname"]=>
    string(4) "Jane"
    ["lastname"]=>
    string(3) "Doe"
  }
}
array(1) {
  [6]=>
  array(12) {
    ["ID"]=>
    string(5) "1003"
    ["created_timestamp"]=>
    string(26) "2015-07-16 19:20:00"
    ["firstname"]=>
    string(4) "John"
    ["lastname"]=>
    string(4) "John"
  }
}

[5]表示当前月份-1。我想要做的是创建一个 table 和 headers 但只有一次,然后用名字和姓氏填充 table。

foreach($m as $key => $value)
            {
                if(key($m) != $currentKey)
                {
                    $currentKey = key($m);
                }
                if($key == date("n", strtotime($value['created_timestamp']))-1);
                {
                    for($i=$currentKey; $i<=$lastkey; $i++) {
                        echo "<br>".count($key);
                        $table .= '<table id=' . date("F-Y", strtotime($m[$currentKey]['created_timestamp'])) . ' class="tblclass">
                        <thead><tr><th>Month</th><th>ID</th><th>Customer</th></tr></thead>';
                        $table .= '<tbody>';
                        $table .= '<tr><td>'.date("F Y", strtotime($m[$currentKey]['created_timestamp'])).'</td><td>' . $value['ID'] . '</td><td>' . $value['lastname'] . ' ' . $value['firstname'] . '</td></tr>';

                        $table .= '</tbody>';
                        $table .= '</table>';
                    }
                }
            }

每当我 运行 代码时,它都会为每个单独的数组创建一个 table。我想为每个单独的月份创建一个 table 并用具有相同密钥的任何内容填充它。我觉得我错过了什么。任何帮助将不胜感激!

一个肮脏的解决方案。由于缺少信息
这里有一些假设:
- 数组按 ASC 顺序排序
- 每个数组中只有一项 [ID, Name, Timestamp]
- 如果有更多的项目,那么你可以在那里添加一个循环
- $m 是多维数组。
注意:未测试。

$currentKey = 0;
$count= 0;
$lastKey = count($m) - 1;

foreach($m as $key => $value) {

    if(key($m) != $currentKey) {
        $currentKey = key($m);

        if($count== 0) {
            echo "<br>".count($key);
            $table .= '<table id=' . date("F-Y", strtotime($m[$currentKey]['created_timestamp'])) . ' class="tblclass">
            <thead><tr><th>Month</th><th>ID</th><th>Customer</th></tr></thead>';
            $table .= '<tbody>';
        } else {            
            $table .= '</tbody>';
            $table .= '</table>';
            $table .= '<table id=' . date("F-Y", strtotime($m[$currentKey]['created_timestamp'])) . ' class="tblclass">
            <thead><tr><th>Month</th><th>ID</th><th>Customer</th></tr></thead>';
            $table .= '<tbody>';
        }
    }

    if($key == date("n", strtotime($value['created_timestamp']))-1) {
        $table .= '<tr><td>'.date("F Y", strtotime($value['created_timestamp'])).'</td><td>' . $value['ID'] . '</td><td>' . $value['lastname'] . ' ' . $value['firstname'] . '</td></tr>';
    }

    if($lastKey == $count) {
        $table .= '</tbody>';
        $table .= '</table>';
    }

    $count++;
}