3v4l.org

run code in 300+ PHP versions simultaneously
<?php function randomizeTeam( int $teamSize, array $positionPool, array $positionMins = [], array $positionMaxs = [] ): array { // guard conditions foreach ($positionMins as $pos => $min) { if (!key_exists($pos, $positionPool) || $positionPool[$pos] < $min) { throw new Exception('Position pool cannot accommodate minimum required positions'); } } // input preparation foreach ($positionMaxs as $pos => $max) { if (key_exists($pos, $positionPool) && $positionPool[$pos] > $max) { $positionPool[$pos] = $max; } } $team = []; $sum = 0; foreach ($positionPool as $pos => &$pool) { $team[$pos] = $positionMins[$pos] ?? 0; $pool -= $team[$pos]; if (!$pool) { unset($positionPool[$pos]); // remove exhausted pool } $sum += $team[$pos]; } while ($sum < $teamSize && $positionPool) { $pos = array_rand($positionPool); ++$team[$pos]; --$positionPool[$pos]; if (!$positionPool[$pos]) { unset($positionPool[$pos]); // remove exhausted pool } ++$sum; } if (array_sum($team) < $teamSize) { throw new Exception('Position pool was exhausted before filling team'); } return $team; } var_export( randomizeTeam( 11, [ 'wicket_keeper' => 2, 'batsman' => 6, 'all_rounders' => 5, 'bowlers' => 5, ], [ 'wicket_keeper' => 1, 'batsman' => 3, 'all_rounders' => 1, 'bowlers' => 2, ], [ 'wicket_keeper' => 3, 'batsman' => 5, 'all_rounders' => 5, 'bowlers' => 4, ] ) );
Output for git.master
array ( 'wicket_keeper' => 2, 'batsman' => 4, 'all_rounders' => 2, 'bowlers' => 3, )
Output for git.master_jit
array ( 'wicket_keeper' => 1, 'batsman' => 3, 'all_rounders' => 3, 'bowlers' => 4, )
Output for rfc.property-hooks
array ( 'wicket_keeper' => 1, 'batsman' => 3, 'all_rounders' => 5, 'bowlers' => 2, )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
73.11 ms | 408 KiB | 5 Q