PHP创建年和月的多层数组

PHP创建年和月的多层数组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我一直在尝试创建一个MySQL数据库的简单前端,它将显示分为年和月目录的表 foreach($tables as $table) { $year = mysqli_query($connect, "SELECT ORDERED_ON FROM $table LIMIT 1") ->fetch_array()[0]; $year = date("Y",strtotime($year)); $month = date("m",strtotime($year));

我一直在尝试创建一个MySQL数据库的简单前端,它将显示分为年和月目录的表

foreach($tables as $table) {
   $year = mysqli_query($connect, "SELECT ORDERED_ON FROM $table LIMIT 1")
->fetch_array()[0];

    $year = date("Y",strtotime($year));
    $month = date("m",strtotime($year));

    $years[$year][$month][] = $table;
}
我想要的是一个年的数组,每年有一个月的子数组,每个月有一个表名的子数组,表名的日期列中有年份和月份

foreach($tables as $table) {
   $year = mysqli_query($connect, "SELECT ORDERED_ON FROM $table LIMIT 1")
->fetch_array()[0];

    $year = date("Y",strtotime($year));
    $month = date("m",strtotime($year));

    $years[$year][$month][] = $table;
}
比如:

foreach($tables as $table) {
   $year = mysqli_query($connect, "SELECT ORDERED_ON FROM $table LIMIT 1")
->fetch_array()[0];

    $year = date("Y",strtotime($year));
    $month = date("m",strtotime($year));

    $years[$year][$month][] = $table;
}
2017年{
01{
表01
表02
}
02{
表03
表04
}
}

foreach($tables as $table) {
   $year = mysqli_query($connect, "SELECT ORDERED_ON FROM $table LIMIT 1")
->fetch_array()[0];

    $year = date("Y",strtotime($year));
    $month = date("m",strtotime($year));

    $years[$year][$month][] = $table;
}
创建此数组结构的最佳方法是什么?我对多级阵列没有太多经验

foreach($tables as $table) {
   $year = mysqli_query($connect, "SELECT ORDERED_ON FROM $table LIMIT 1")
->fetch_array()[0];

    $year = date("Y",strtotime($year));
    $month = date("m",strtotime($year));

    $years[$year][$month][] = $table;
}
我已经得到了一个仅包含MySQL表中的年份的简单数组,所以我只需要将月份添加到该数组中,然后在其后添加表名

foreach($tables as $table) {
   $year = mysqli_query($connect, "SELECT ORDERED_ON FROM $table LIMIT 1")
->fetch_array()[0];

    $year = date("Y",strtotime($year));
    $month = date("m",strtotime($year));

    $years[$year][$month][] = $table;
}
MySQL中的每个表都被命名为_ProductID,ID是一个六位数的数字。它包含给定月份该产品的所有订单的列表。因此,2017年1月的ID/表名可能是123456,但2月的ID/表名可能是123457

foreach($tables as $table) {
   $year = mysqli_query($connect, "SELECT ORDERED_ON FROM $table LIMIT 1")
->fetch_array()[0];

    $year = date("Y",strtotime($year));
    $month = date("m",strtotime($year));

    $years[$year][$month][] = $table;
}
每个表都有一个日期格式为YYYY-MM-DD的有序列,因此我选择年份,并在所有表中运行foreach语句:

foreach($tables as $table) {
    $year = mysqli_query($connect, "SELECT ORDERED_ON FROM $table LIMIT 1")
    ->fetch_array()[0];

    if(!in_array(explode('-', $year)[0], $years, true)) {
        array_push($years, explode('-', $year)[0]);
    }
}
foreach($tables as $table) {
   $year = mysqli_query($connect, "SELECT ORDERED_ON FROM $table LIMIT 1")
->fetch_array()[0];

    $year = date("Y",strtotime($year));
    $month = date("m",strtotime($year));

    $years[$year][$month][] = $table;
}

您应该利用“strotime”,它在PHP中是一个非常强大的函数。它允许您转换日期并使用date()函数对其进行操作,如下所示。 最重要的是,PHP关联数组非常容易动态创建。因为一次只添加一个元素,所以不需要真正的数组推送

foreach($tables as $table) {
   $year = mysqli_query($connect, "SELECT ORDERED_ON FROM $table LIMIT 1")
->fetch_array()[0];

    $year = date("Y",strtotime($year));
    $month = date("m",strtotime($year));

    $years[$year][$month][] = $table;
}

我希望这能有所帮助。

我设法找到了实现我想要的东西的方法。我首先运行一个mysqli_查询来收集所有表名并将它们添加到一个数组中,然后使用foreach循环遍历每个表并将其添加到总日期数组中:

foreach($tables as $table) {
   $year = mysqli_query($connect, "SELECT ORDERED_ON FROM $table LIMIT 1")
->fetch_array()[0];

    $year = date("Y",strtotime($year));
    $month = date("m",strtotime($year));

    $years[$year][$month][] = $table;
}
$structure = [];

foreach($wowcherTables as $tableName) { 
    $redeemed = mysqli_query($connect, "SELECT Redeemed_At FROM wowcher.$tableName LIMIT 1")
                ->fetch_array()[0];

    $year = explode('-', $redeemed)[0];
    $month = explode('-', $redeemed)[1];

    $structure[$year][$month] = array();
}
第一个foreach语句从每个表的第一行获取年和月,并在$structure数组中创建这些层

foreach($tables as $table) {
   $year = mysqli_query($connect, "SELECT ORDERED_ON FROM $table LIMIT 1")
->fetch_array()[0];

    $year = date("Y",strtotime($year));
    $month = date("m",strtotime($year));

    $years[$year][$month][] = $table;
}
然后,我必须执行相同的foreach语句,使用array_push将每个表名添加到数组中,因为出于某种原因,当我刚刚将array_push添加到单个foreach中时,每个月只会向其中添加一个表名

foreach($tables as $table) {
   $year = mysqli_query($connect, "SELECT ORDERED_ON FROM $table LIMIT 1")
->fetch_array()[0];

    $year = date("Y",strtotime($year));
    $month = date("m",strtotime($year));

    $years[$year][$month][] = $table;
}
foreach($wowcherTables as $tableName) { 
    $redeemed = mysqli_query($connect, "SELECT Redeemed_At FROM wowcher.$tableName LIMIT 1")
                ->fetch_array()[0];

    $year = explode('-', $redeemed)[0];
    $month = explode('-', $redeemed)[1];

    array_push($structure[$year][$month], $tableName);
}

如果有人能向我解释发生这种情况的原因以及一种更有效的方法来添加表,这将非常有用,因为有两个相同的foreach语句似乎是多余和不必要的。

给我们一些关于mysql中表的信息。以及如何实际检索它们。您必须向我们展示您的代码。我已经添加了一些关于表的更多信息,以及我如何创建仅相关年份的数组(如果有用的话)。
foreach($tables as $table) {
   $year = mysqli_query($connect, "SELECT ORDERED_ON FROM $table LIMIT 1")
->fetch_array()[0];

    $year = date("Y",strtotime($year));
    $month = date("m",strtotime($year));

    $years[$year][$month][] = $table;
}