I am looking for a slot filling technique. I have 8 "slots" represented as a flat array. I also have a multidimensional array where keys are subjects and values are subarrays containing indexed elements (slots).
The multidimensional array ($emp) may contain, for example, the subject Tamil which is available at slots 1 through 5, and/or English which is only available during slots 4, 5, or 6. All subjects on offer will have an array of available slots -- even if only one slot is available.
Subject Availability:
$emp = array(
"tamil" => array(1, 2, 3, 4, 5),
"english" => array(4, 5, 6),
"maths" => array(1, 5, 8),
"social" => array(1, 2, 5),
"pt" => array(1),
"hindi" => array(3, 4, 7)
);
I need to assign the following subjects the exact number of times -- while obeying subject availability AND without encountering a "slot conflict" -- in my output array ($sort):
- Tamil 2 times
- PT 1 time
- Maths 2 times
- Social 1 time
- Hindi 1 time
- English 1 time
All 8 slots must be filled. All subjects must occur only once with exception to Tamil and Math which must occur exactly twice.
Output may be:
$slot[3] = 'tamil',
$slot[7] = 'Hindi',
$slot[6] = 'English',
$slot[1] = 'Pt',
$slot[5] = 'Maths',
$slot[8] = 'Maths',
$slot[4] = 'Tamil',
$slot[2] = 'Social'.
I have tried to code this myself, but I was unsuccessful.
for ($i = 1; $i < 9; $i++) {
$var = $subarray[$i];
if (count($var) <= 1) {
$filledslots[$i] = $var[0];
} else {
$empty[$i] = '';
}
}
$subjects = array_flat($subarray);
$count_values = array();
$subjects = array_diff($subjects, $filledslots);
foreach ($subjects as $value) {
$count_values[$value] = 0;
foreach ($empty as $key => $val) {
$res = $subarray[$key];
if (in_array($value, $res)) {
$count_values[$value] = $count_values[$value] + 1;
}
}
if ($count_values[$value] == 1) {
//$filledslots[1] = $count_values[$value];
foreach ($empty as $key => $val) {
$res = $subarray[$key];
if (in_array($value, $res)) {
$filledslots[$key] = $value;
}
}
}
}
print_r(($filledslots));