I’m working on a nine player, three team drafting project. I’m trying to get the logic sorted. However, I’m running into two concerns that I’m hoping to get some help with.
I have an array of players. The player name is set as the key and the player type is set as the value. Here’s what it looks like:
$players = array("Player 1" => "Mentor","Player 2" => "Mentor","Player 3" => "Mentor","Player 4" => "Player","Player 5" => "Player","Player 6" => "Player","Player 7" => "Player","Player 8" => "Player");
There should be a total of 3 teams, and each team should only have 1 “Mentor” on it.
My first concern is that my array output isn’t correctly grabbing the player name.
My second concern is that I’m not sure how to logically set 1 Mentor per team.
After looking at a few posts, I know array_chunk can use the option of true for the third setting to store the previous keys, but it still doesn’t seem to be working. Here’s the entire script so far:
$players = array("Player 1" => "Mentor","Player 2" => "Mentor","Player 3" => "Mentor","Player 4" => "Player","Player 5" => "Player","Player 6" => "Player","Player 7" => "Player","Player 8" => "Player");
shuffle($players);
$max_players = 3;
$teams = array_chunk($players,$max_players,true);
$teamno = 1;
for ($i = 0; $i < $max_players; $i++) {
$teamcheck = $teams[$i];
echo 'Team '.$teamno.'<br>';
foreach ($teamcheck as $name => $type) {
echo $name;
}
echo "<br><br>";
$teamno++;
}
For my first concern that the output isn’t correctly grabbing the player name, it seems to be grabbing key values of 0-7, without the shuffle. It outputs the following:
Team 1
012
Team 2
345
Team 3
67
However, if I change $name to $type in the echo, it correctly shuffles, but only shows the player type, and not name. It outputs the following:
Team 1
PlayerMentorPlayer
Team 2
MentorPlayerMentor
Team 3
PlayerPlayer
How can I get it to correctly show the player name, and it shuffles correctly?
How can I also implement logic so that if a Mentor type already exists in that chunk portion, only bring in two other Player types, then move on?
Mitchelson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.