2016-11-24 65 views
-1

为学校项目创建修订时间表,让用户在为每个项目选择主题和多少小时之前,将其输入到2d数组中,然后将其用作表格。我已经写了下面的代码,通过前一页的帖子获取数组$ 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”,但我不知道。

+1

只是一个提示:'返回$计数<$科目[$主题]'是一样的'如果($计数< $ subjects [$ subject]){return True; } else {return false; ''但更好的可读性,可能更快。 – Christoph

+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, '')); 

结构完全相同的数组作为您的多头排列()语句。

一个更大的附加: 你的代码可以重构这个:

$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 
+0

谢谢@Christoph。这完全解决了这个问题。数组现在被填充,但并不总是正确的数量。例如,如果你使用2科目数学(9)和物理(6)。该数组将正确填充第一个数字,即数学的9个实例,但看似随机数量的第二个科目即只有1个物理学。 –

+1

@Adam Anderson我已经添加了6行代码,它们与您当前的代码/您要实现的内容相当,但如果您希望我进一步搜索您的bug,请告诉我。 – Christoph

+1

@Adam Anderson我的答案现在包含足够的错误以使您的代码正常工作。随时接受它或提出进一步的问题:) – Christoph