I'm trying to make a program that has different players in an array assigned with random numbers. It is round-based, so every round the players are assigned new random numbers, and the player(s) with the lowest number is removed from the array.
Problem is that when iterating it doesn't remove the player with the lowest number from the array. It does however print out the player with the lowest number, but that's it...
I want it to remove the player or players and display their names each round till there is one left.
I'm new to learning PHP, so my code structure is not the best xd
$players = array(
"Peter" => "0",
"John" => "0",
"Harold" => "0",
"Alexander" => "0",
"Thor" => "0",
"Paul" => "0",
"Jimmy" => "0",
"Erik" => "0",
"Donald" => "0",
"Matthew" => "0"
);
for($i = 0; $i < count($players); $i++){
echo "<br>" . "<b>Round ". (1 + $i) ."</b><br>";
foreach($players as $key => $value){
//generating random number to value
$value = rand(1,50);
asort($players);
//Assigning each player a random number
$players[$key]=$value;
$min = min($players);
array_splice($players,$min));
}
echo "Player(s) with lowest number is: " .current(array_keys($players, min($players)));
echo "<br>"
}
Instead of usefor-loop
i suggest you to usewhile
like:
$players = array(
"Peter" => "0",
"John" => "0",
"Harold" => "0",
"Alexander" => "0",
"Thor" => "0",
"Paul" => "0",
"Jimmy" => "0",
"Erik" => "0",
"Donald" => "0",
"Matthew" => "0"
);
$round = 1;
while (count($players) > 1) {
echo "<br>" . "<b>Round " . $round . "</b><br>";
foreach ($players as $key => $value) {
do {
$value = rand(1, 50);
} while(in_array($value, $players));
$players[$key] = $value;
}
$key = array_keys($players, min($players))[0];
echo "Player(s) with lowest number is: " . $key;
echo "<br>";
unset($players[$key]);
$round++;
}
echo '<br>The winner is:'. key($players);
What have I changed?
while
with logic "continue while untile array count is one" (so just the winnerdo-while
for generate different number for each player (unique number)min()
of array then unset it.Reference:
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.