Search code examples
phparraysfunctionforeachinvalid-argument

Is "array_count_values" the the correct function to use?


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) {

Solution

  • 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]);
        }
      }
    }