Adam Anderson Adam Anderson - 15 days ago 5
PHP Question

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

Answer

while (Check_end($subjects, $timetable == TRUE)) {, you are comparing $timetable == TRUE as the second argument to the Check_end function meaning you are passing in a boolean, not an array. You likely want to move the closing parenthesis from the end of the line to after $timetable so you pass in timetable and compare the return value to true like while (Check_end($subjects, $timetable) == TRUE) {

Reading over the errors again, this seems like it would fix all the issues which stem from $timetable not being an array. You can't foreach loop over a true/false, you can't access Maths and it is a boolean so there is no offset 2 or 4, even if coerced to a string.