我为学校项目创建了一个修订时间表,让用户可以选择主题以及每个主题需要多少小时,然后将它们输入到用户可以用作表格的二维数组中。我编写了以下代码,通过上一页的帖子获取数组$ subject。
$ subject是一个二维数组,第一级是用户选择的主题,第二级是用户想要该主题的小时数。
代码应该使用$ subject数组并在许多函数中使用它来填充数组但是当我运行代码时我只得到一个空数组
这是代码
<?php
$timetable = array(
"0" => array // 0 = Monday 6 = Sunday
// 0 - 23 = horus
(
"0" => '',
"1" => '',
"2" => '',
"3" => '',
"4" => '',
"5" => '',
"6" => '',
"7" => '',
"8" => '',
"9" => '',
"10" => '',
"11" => '',
"12" => '',
"13" => '',
"14" => '',
"15" => '',
"16" => '',
"17" => '',
"18" => '',
"19" => '',
"20" => '',
"21" => '',
"22" => '',
"23" => ''
),
"1" => array
(
"0" => '',
"1" => '',
"2" => '',
"3" => '',
"4" => '',
"5" => '',
"6" => '',
"7" => '',
"8" => '',
"9" => '',
"10" => '',
"11" => '',
"12" => '',
"13" => '',
"14" => '',
"15" => '',
"16" => '',
"17" => '',
"18" => '',
"19" => '',
"20" => '',
"21" => '',
"22" => '',
"23" => ''
),
"2" => array
(
"0" => '',
"1" => '',
"2" => '',
"3" => '',
"4" => '',
"5" => '',
"6" => '',
"7" => '',
"8" => '',
"9" => '',
"10" => '',
"11" => '',
"12" => '',
"13" => '',
"14" => '',
"15" => '',
"16" => '',
"17" => '',
"18" => '',
"19" => '',
"20" => '',
"21" => '',
"22" => '',
"23" => ''
),
"3" => array
(
"0" => '',
"1" => '',
"2" => '',
"3" => '',
"4" => '',
"5" => '',
"6" => '',
"7" => '',
"8" => '',
"9" => '',
"10" => '',
"11" => '',
"12" => '',
"13" => '',
"14" => '',
"15" => '',
"16" => '',
"17" => '',
"18" => '',
"19" => '',
"20" => '',
"21" => '',
"22" => '',
"23" => ''
),
"4" => array
(
"0" => '',
"1" => '',
"2" => '',
"3" => '',
"4" => '',
"5" => '',
"6" => '',
"7" => '',
"8" => '',
"9" => '',
"10" => '',
"11" => '',
"12" => '',
"13" => '',
"14" => '',
"15" => '',
"16" => '',
"17" => '',
"18" => '',
"19" => '',
"20" => '',
"21" => '',
"22" => '',
"23" => ''
),
"5" => array
(
"0" => '',
"1" => '',
"2" => '',
"3" => '',
"4" => '',
"5" => '',
"6" => '',
"7" => '',
"8" => '',
"9" => '',
"10" => '',
"11" => '',
"12" => '',
"13" => '',
"14" => '',
"15" => '',
"16" => '',
"17" => '',
"18" => '',
"19" => '',
"20" => '',
"21" => '',
"22" => '',
"23" => ''
),
"6" => array
(
"0" => '',
"1" => '',
"2" => '',
"3" => '',
"4" => '',
"5" => '',
"6" => '',
"7" => '',
"8" => '',
"9" => '',
"10" => '',
"11" => '',
"12" => '',
"13" => '',
"14" => '',
"15" => '',
"16" => '',
"17" => '',
"18" => '',
"19" => '',
"20" => '',
"21" => '',
"22" => '',
"23" => ''
)
);
$subjects = $_POST;
function pick_random_subject($subjects, $timetable)
{
$available = FALSE;
while ($available == FALSE) {
$subject = array_rand($subjects);
if (check_subject_availability($subjects, $timetable, $subject)) {
$available = TRUE;
}
}
return $subject;
}
function check_subject_availability($subjects, $timetable,$subject)
{
$count = 0;
foreach ($timetable as $day) {
$count += array_count_values($day)[$subject];
}
if ($count < $subjects[$subject]) {
return True;
} else {
return false;
}
}
function verify_available_slot($timetable, $day, $slot)
{
if ($timetable[$day][$slot] == '') {
return true;
} else {
return false;
}
}
function pick_random_slot($timetable)
{
$available = FALSE;
while ($available == FALSE) {
$day = rand(0, 6);
$hour = rand(0, 23);
$available = verify_available_slot($timetable, $day, $hour);
}
return [$day, $hour];
}
function Check_end($subjects, $timetable)
{
$finished = FALSE;
foreach ($subjects as $subject) {
if (!check_subject_availability($subjects, $timetable, $subject)) {
$finished = TRUE;
break;
}
}
return $finished;
}
if(isset($_POST)) {
while(Check_end($subjects, $timetable )== FALSE)
{
$subject = pick_random_subject($subjects, $timetable);
$slot = pick_random_slot($subject);
$day = $slot[0];
$hour = $slot[1];
$timetable[$day][$hour] = $subject;
}
}
else {
header('http://localhost/timetable/TimetableAlgorithmn.php');
}
?>
<pre>
<?print_r($timetable) ?>
<pre>
注意:我认为问题在于“check_subject_availability”函数,但我不确定。
答案 0 :(得分:2)
如果没有周围的代码我无法测试,但至少有一个问题应该在Check_end()
和check_subject_availability
来电之间:
function Check_end($subjects, $timetable)
{
$finished = FALSE;
foreach ($subjects as $subject) {
if (!check_subject_availability($subjects, $timetable, $subject)) {
$finished = TRUE;
break;
}
}
return $finished;
}
此代码假定主题的名称是一个值,例如$subjects == ['subject1']
在check_subject_availability()
中,您将其用作主题的关键,例如$subjects == ['subject1' => 5]
function check_subject_availability($subjects, $timetable,$subject)
{
$count = 0;
foreach ($timetable as $day) {
$count += array_count_values($day)[$subject];
}
return $count < $subjects[$subject]; // usage as key
}
将foreach ($subjects as $subject)
更改为foreach ($subjects as $subject => $max_count)
可能会解决您的问题。
另一个错误:
while(Check_end($subjects, $timetable )== FALSE)
{
$subject = pick_random_subject($subjects, $timetable);
list($day, $hour) = pick_random_slot($timetable); // $timetable not $subject
$timetable[$day][$hour] = $subject;
}
最后是最后一个错误(至少让它为我工作:)。
function Check_end($subjects, $timetable)
{
$finished = TRUE;
foreach ($subjects as $subject => $max_n) {
if (check_subject_availability($subjects, $timetable, $subject)) {
$finished = false;
}
}
return $finished;
}
只要有一个主题,您的版本就会停止!check_subject_availability()。仅当所有主题都不可用时,此版本才会停止。
最后一个提示:您应该考虑我在下面的代码中使用的随机播放方法,因为当时间表填满并且您再也找不到空插槽时,您的版本会慢慢出现问题。
稍加一点:
$timetable = array_fill(0, 7, array_fill(0, 24, ''));
构造与long array()语句完全相同的数组。
更大的补充: 您的代码可以重构为:
$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, 24 * 7 - count($timetable), '')); // fill the array with empty values
shuffle($timetable); // shuffle the set
$timetable = array_chunk($timetable, 24); // split it into 7 days