Random NON subteams generator - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.

Name: Random NON subteams generator fullscreencopydownloadembedprint


Your result can be seen below.

Result of php executing





Full code of Random NON subteams generator.php

  1. <?php
  2. $arr = ["Kasia", "Marcin", "Erik", "Wojtek"];
  3. $teamsNo = 4;
  4. $shiftMembers = 2;
  5.  
  6. $shifts = [];
  7. for ($i=0; $i < $teamsNo; $i++) {
  8.     shuffle($arr);
  9.     if (!count($shifts)) {
  10.         $shifts[] = [$arr[0],$arr[1]];
  11.     } else {
  12.         $lastShift = $shifts[count($shifts)-1];
  13.         $currentShift = [];
  14.         for ($k=0; $k < count($arr); $k++) {
  15.             if (count($currentShift) == 0 && !in_array($arr[$k], $lastShift)) {
  16.                 $currentShift[] = $arr[$k];
  17.             } elseif (count($currentShift) > 0) {
  18.                 $currentShift[] = $arr[$k];
  19.             }
  20.             if (count($currentShift) == $shiftMembers) {
  21.                 $shifts[] = $currentShift;
  22.                 break;
  23.             }
  24.         }
  25.     }
  26. }
  27.  
  28. for ($i =0; $i < count($shifts); $i++) {
  29.     echo 'Week '. ($i+1) . ': ' . $shifts[$i][0] . ' and ' . $shifts[$i][1].PHP_EOL;
  30. }
  31.  
  32.  
File Description
  • Random NON subteams generator
  • PHP Code
  • 18 Oct-2019
  • 870 Bytes
You can Share it: