Php 未放置在数组中的值

Php 未放置在数组中的值,php,arrays,function,multidimensional-array,Php,Arrays,Function,Multidimensional Array,我正在为一个学校项目创建一个修订时间表,让用户选择科目以及每个科目需要多少小时,然后将它们输入到用户可以用作表格的2d数组中。我编写了以下代码,通过上一页的文章获取数组$subject $subject是一个2d数组,第一个级别是用户选择的主题,第二个级别是用户希望为该主题花费多少小时 代码应该使用$subject数组,并在许多函数中使用它来填充数组,但当我运行代码时,只会得到一个空数组 这是密码 <?php $timetable = array( "0" => array

我正在为一个学校项目创建一个修订时间表,让用户选择科目以及每个科目需要多少小时,然后将它们输入到用户可以用作表格的2d数组中。我编写了以下代码,通过上一页的文章获取数组$subject

$subject是一个2d数组,第一个级别是用户选择的主题,第二个级别是用户希望为该主题花费多少小时

代码应该使用$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\u subject\u的可用性,但我不确定。

如果没有周围的代码,我无法测试它,但check\u end和check\u subject\u可用性调用之间至少应该存在一个问题:

此代码假定主题的名称是一个值,例如$subjects==['subject1']

在check_subject_availability中,您使用它作为主题的键,例如$subjects=['subject1'=>5]

最后一个bug,至少是让它为我工作的bug:

$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
一个主题被删除,你的版本就停止了!检查主题的可用性。仅当所有主题均不可用时,此版本才会停止

最后一个提示:你应该考虑下面代码中使用的洗牌方法,因为当时间表填满时,你的版本会慢慢地出现问题,而且你再也找不到空插槽了。 再补充一点:

构造与长数组语句完全相同的数组

更大的补充: 您的代码可以重构为:


提示:返回$count<$subject[$subject];与if$count<$subjects[$subject]{return True;}else{return false;}相同,但可读性更好,可能更快。也许您可以使用for或for来创建这个巨大的结构?你的代码是巨大的!谢谢你,克里斯托夫。这几乎完全解决了这个问题。阵列现在已填充,但并不总是正确的数量。例如,如果你使用两门学科:数学和物理。数组将正确填充第一个,即9个数学实例,但第二个主题的数量似乎是随机的,即只有1个物理。@Adam Anderson我添加了6行代码,这些代码相当于您当前的代码/您试图实现的目标,但如果您希望我进一步搜索您的错误,请告诉我。@Adam Anderson我的答案现在包含了足够的错误,可以让您的代码正常工作。请随意接受它或提出进一步的问题:非常感谢您,您发布的代码非常有效。我不得不将数组_shuffle改为shuffle@AdamAnderson当然当然是洗牌。我好久没有php了;我希望您理解使用shuffle而不是rand的方法
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
}
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;
}
$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