public function addUserToTeamWithLessPlayers($user_id, $match_id) { $players_in_teams = Utilities::getPlayersInTeams($this->getAllMatchPlayers($match_id)); //-- We should Have two teams now $count_of_first_team_players = count(reset($players_in_teams)); $count_of_second_team_players = count(end($players_in_teams)); if ($count_of_first_team_players >= $count_of_second_team_players) { $add_to_match_team_id = array_keys($players_in_teams)[1]; } else { $add_to_match_team_id = array_keys($players_in_teams)[0]; } Match::assignMatchTeamIdToPlayers($match_id, $add_to_match_team_id, [$user_id]); }