dongyue1988 2016-11-23 18:34
浏览 51
已采纳

“array_count_values”是否使用正确的函数?

Im making a revision timetable generating program for a school project. I have tried to create a number of functions to do this using a 2d-array but I receive many errors and think its to do with the library function 'array_count_values' that I have used within a function checking if the subject is available for use.

here is the code that checks the 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;
}
}

I think this is the root of the problem but here is the rest of my code that may be causing the issue

$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] == null) {
    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 == TRUE)) {

    $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');
}
var_dump($timetable)
?>
<pre>
<? print $timetable ?>
<pre>

The code is supposed to take the values of subjects given through post as a 2d-array,ie maths 2, physics 3. and assign the values maths and physics to the array until they have each been used the specified amount of times. The 'check_subject_availability' function is meant to see if the subject has been used the specified amount of times and return true of false.Sorry in advance for the badly formatted question and badly written code.

Here is a screenshot of the errors I encounter when the code is run: screenshot

The lines refer to these pieces of code

line 29

foreach ($timetable as $day) {

Line 30

$count += array_count_values($day)[$subject];

Line 33

if ($count < $subjects[$subject]) {

Line 42

if ($timetable[$day][$slot] == null) {
  • 写回答

2条回答 默认 最新

  • douwei7203 2016-11-23 18:39
    关注

    You probably want count() in line 30, but also note that if $day[$subject] is an array there is another syntax error there.

    $count = count($day[$subject]);
    

    Also, get rid of the warning on line 29 like this:

    if (is_array($timetable)) {
      foreach ($timetable as $day) {
        if (!empty($day[$subject])) {
          $count += count($day[$subject]);
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么