【发布时间】:2017-01-19 09:25:07
【问题描述】:
我有以下代码以多维数组的形式创建修订时间表。每个部分代表一天和每小时一个要修改的时间段。我试图将这个多维数组转换为 mysql 中的一个表,以便时间表对用户友好并且可以保存等等。这是用于生成时间表以及生成的时间表的代码。
<?php
if(!isset($_POST['subjects'])) {
header('Location: index.php');
}
define('DAY_LENGTH', 12);
$timetable = [];
foreach ($_POST as $subject => $n) { // add in the required amount of subjects
$timetable = array_merge($timetable, array_fill(0, $n, $subject));
}
$timetable = array_merge($timetable, array_fill(0, DAY_LENGTH * 7 - count($timetable), '')); // fill the array with empty values
shuffle($timetable); // shuffle the set
$timetable = array_chunk($timetable, DAY_LENGTH); // split it into 7 days
echo '<pre>';
var_dump($timetable);
echo '</pre>';
这是数组结构,它有 7 个子数组,但我只显示了前 2 个以节省时间。每个子数组代表一周中的一天,即 [0] 将是星期一,其中的每个数组是一天中的时间,后跟一个主题,即 [8] = 8pm 和物理
array(7) {
[0]=>
array(12) {
[0]=>
string(0) ""
[1]=>
string(9) "Computing"
[2]=>
string(7) "Physics"
[3]=>
string(7) "Physics"
[4]=>
string(7) "Physics"
[5]=>
string(5) "Maths"
[6]=>
string(7) "Physics"
[7]=>
string(7) "Physics"
[8]=>
string(7) "Physics"
[9]=>
string(7) "Physics"
[10]=>
string(7) "Physics"
[11]=>
string(7) "Physics"
}
[1]=>
array(12) {
[0]=>
string(7) "Physics"
[1]=>
string(9) "Computing"
[2]=>
string(7) "Physics"
[3]=>
string(7) "Physics"
[4]=>
string(7) "Physics"
[5]=>
string(7) "Physics"
[6]=>
string(5) "Maths"
[7]=>
string(7) "Physics"
[8]=>
string(7) "Physics"
[9]=>
string(7) "Physics"
[10]=>
string(7) "Physics"
[11]=>
string(5) "Maths"
}
【问题讨论】:
-
你需要展示你尝试了什么以及你的桌子是什么样子
-
我真的不知道从哪里开始 tbh,我对 sql 很陌生,可以做非常简单的功能,但我不知道如何处理这个数组
-
您是否设计/创建了数据库架构?
-
@RamRaider 我真的没有准备好结构,但它只包含 2 个表,一个包含用户登录系统,一个存储用户时间表的数据(id/day/subject/timeslot ) 用 id 链接它们。它可能不是第三范式或根本不正确
标签: php mysql arrays multidimensional-array