Welcome to today's lesson! We'll be exploring how to manage student enrollments for various courses using PHP. Imagine you're running an online course platform and need to handle enrollments, checks, and listings of students in different courses. PHP's associative arrays are perfect for this kind of problem since they allow straightforward management of unique student enrollments per course.
By the end of this session, you'll be well-versed in using PHP associative arrays for such tasks. Let’s dive in!
Introducing Methods to Implement
Here are the methods we need to implement in our enrollment system:
function enroll($student, $course): Adds a student to a course. If the student is already enrolled, it does nothing.
function unenroll($student, $course): Removes a student from a course. Returns true if the student was enrolled and has now been removed, otherwise returns false. If, after unenrolling the student, the course becomes empty (no one is enrolled there), remove the course as well.
function isEnrolled($student, $course): Checks if a student is enrolled in a course. Returns true if the student is enrolled and false otherwise.
function listStudents($course): Returns an array of all students enrolled in a given course. If no students are enrolled, it returns an empty array.
Let's look at how to implement each of these functions step-by-step.
Step 1: Define the Class
We'll start by defining our class and then add each method one by one.
This code initializes an EnrollmentSystem class with an associative array named enrollments that maps courses to arrays of students.
Step 2: Implement 'enroll' Method
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal
Next, implement the enroll method:
PHP
<?phpclass EnrollmentSystem { private $enrollments = []; public function enroll($student, $course) { if (!isset($this->enrollments[$course])) { $this->enrollments[$course] = []; } if (!in_array($student, $this->enrollments[$course])) { $this->enrollments[$course][] = $student; } }}// Example usage:$es = new EnrollmentSystem();$es->enroll("Alice", "Math101");?>
Here, the enroll function uses in_array to check if the student is already enrolled. If not, it adds the student to the array of students for the specified course.
Step 3: Implement 'unenroll' Method
Step 4: Implement 'isEnrolled' Method
Step 5: Implement 'listStudents' Method
Lesson Summary
In today's lesson, we learned how to manage student enrollments using PHP associative arrays and classes. We implemented functions to enroll and unenroll students, check enrollments, and list students in a course. This task provided a practical way to reinforce your understanding of associative arrays and class structure in PHP.
I encourage you to move on to the practice to undertake similar challenges to deepen your understanding. Keep experimenting and honing your skills. Happy coding!
<?phpclass EnrollmentSystem { private $enrollments = []; public function enroll($student, $course) { if (!isset($this->enrollments[$course])) { $this->enrollments[$course] = []; } if (!in_array($student, $this->enrollments[$course])) { $this->enrollments[$course][] = $student; } } public function unenroll($student, $course) { if (isset($this->enrollments[$course]) && ($key = array_search($student, $this->enrollments[$course])) !== false) { unset($this->enrollments[$course][$key]); if (empty($this->enrollments[$course])) { unset($this->enrollments[$course]); } return true; } return false; } public function unenroll($student, $course) { // Find the student within the course if (isset($this->enrollments[$course]) && ($key = array_search($student, $this->enrollments[$course])) !== false) { // Remove the student unset($this->enrollments[$course][$key]); // Remove the course if no students are left if (empty($this->enrollments[$course])) { unset($this->enrollments[$course]); } return true; } return false; }}// Example usage:$es = new EnrollmentSystem();$es->enroll("Alice", "Math101");$es->enroll("Bob", "Math101");$es->unenroll("Alice", "Math101");$es->unenroll("Bob", "Math101");?>
This function checks whether the course and student exist in the enrollments. If they do, it removes the student from the course. If the course array becomes empty after removal, it deletes the course from the enrollments. The function returns true if the student was successfully unenrolled and false otherwise.
Note: In the unenroll method, we use !== false to check the result of array_search($student, $this->enrollments[$course]). This is crucial because the search result can be 0 (for the first element in the array), which is a valid index but evaluates to false in a loose comparison. Using !== false ensures we correctly handle cases where the student is found at index 0.
Next, implement the isEnrolled method:
PHP
<?phpclass EnrollmentSystem { private $enrollments = []; public function enroll($student, $course) { if (!isset($this->enrollments[$course])) { $this->enrollments[$course] = []; } if (!in_array($student, $this->enrollments[$course])) { $this->enrollments[$course][] = $student; } } public function unenroll($student, $course) { if (isset($this->enrollments[$course]) && ($key = array_search($student, $this->enrollments[$course])) !== false) { unset($this->enrollments[$course][$key]); if (empty($this->enrollments[$course])) { unset($this->enrollments[$course]); } return true; } return false; } public function isEnrolled($student, $course) { return isset($this->enrollments[$course]) && in_array($student, $this->enrollments[$course]); }}$es = new EnrollmentSystem();$es->enroll("Alice", "Math101");var_dump($es->isEnrolled("Alice", "Math101")); // Output: bool(true)var_dump($es->isEnrolled("Bob", "Math101")); // Output: bool(false)?>
This method checks whether both the course and the student exist in the enrollments array, returning true if they do and false otherwise.
Finally, implement the listStudents method:
PHP
<?phpclass EnrollmentSystem { private $enrollments = []; public function enroll($student, $course) { if (!isset($this->enrollments[$course])) { $this->enrollments[$course] = []; } if (!in_array($student, $this->enrollments[$course])) { $this->enrollments[$course][] = $student; } } public function unenroll($student, $course) { if (isset($this->enrollments[$course]) && ($key = array_search($student, $this->enrollments[$course])) !== false) { unset($this->enrollments[$course][$key]); if (empty($this->enrollments[$course])) { unset($this->enrollments[$course]); } return true; } return false; } public function isEnrolled($student, $course) { return isset($this->enrollments[$course]) && in_array($student, $this->enrollments[$course]); } public function listStudents($course) { if (isset($this->enrollments[$course])) { return array_values($this->enrollments[$course]); } return []; }}$es = new EnrollmentSystem();$es->enroll("Alice", "Math101");$es->enroll("Bob", "Math101");print_r($es->listStudents("Math101")); // Output: Array ( [0] => Alice [1] => Bob )?>
This method returns an array of students enrolled in the given course. If the course is not in the enrollments associative array, it returns an empty array.