Advertisement
ShadowEmbrace

Courses

Nov 12th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.86 KB | None | 0 0
  1. <?php
  2.  
  3. $coursesStudents = [];
  4. $input = readline();
  5.  
  6. while ($input !== 'end') {
  7.     $cmd = explode(' : ', $input);
  8.     $courseName = $cmd[0];
  9.     $studentName = $cmd[1];
  10.  
  11.     if (!key_exists($courseName, $coursesStudents)) {
  12.         $coursesStudents[$courseName] = [];
  13.     }
  14.     $coursesStudents[$courseName][] = $studentName;
  15.  
  16.     $input = readline();
  17. }
  18.  
  19. uksort($coursesStudents, function ($key1, $key2) use ($coursesStudents) {
  20.     $countCourses1 = count($coursesStudents[$key1]);
  21.     $countCourses2 = count($coursesStudents[$key2]);
  22.     return $countCourses2 <=> $countCourses1;
  23. });
  24.  
  25. foreach ($coursesStudents as $course => $students) {
  26.     asort($coursesStudents[$course]);
  27.     echo $course . ": " . count($coursesStudents[$course]) . PHP_EOL;
  28.     foreach ($coursesStudents[$course] as $student) {
  29.         echo "-- $student" . PHP_EOL;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement