1

I'm new to php and I wrote a code that takes a random winner out of an array and displays it, then I wanted to try to display all the names without the winner's, but how can I unset the winner by using the random number generated? I managed to unset a random name but not the same as the winner...

    <?php

// array

$winner = array();
    array_push($winner,"sonia");
    array_push($winner,"ice");
    array_push($winner,"sasha");
    array_push($winner,"isa");
    array_push($winner,"omar");
    array_push($winner,"anwar");

// random winner

    $giocatori = count($winner);   
        $random = rand(0,$giocatori -1);   
            unset($winner[$random]);

// Sort the list

sort($winner);
    echo "I giocatori sono: ";
        print join(", ",$winner);

// Print the winner's name in ALL CAPS

$truewinner = strtoupper($winner[$random]);
    echo "<br><br>";
    echo "il vincitore è: ";

// Print winner + sentence   

$losers = "losers";
$losers = strtoupper($losers);    
    echo (strtoupper($winner[$random]));
    echo "<br><br>";
    echo "all the players are: $losers beside $truewinner"; 

?>
1
  • And to explain why your code did not work: You got a random key from the array and the next operation is to unset it, this mean that you deleted the key and value from the array, later on you want to use the key to get the value and display it, after you deleted it. Commented Sep 29, 2016 at 13:16

3 Answers 3

1
$players = array('sonia', 'ice', 'sasha', 'isa', 'omar');
$random = rand(0, count($players) - 1);
$winner = $players[$random];
$losers = array_diff($players, array($winner));

echo 'All players are: ' . implode(', ', $players);
echo 'Winner is: ' . $winner;
echo 'Losers are: ' . implode(', ', $losers);
Sign up to request clarification or add additional context in comments.

3 Comments

Why would you make array_diff on an array where you already know the key??? Also PHP has a function array_rand();
I kept some things he wrote but there are a lot of other ways to do this, some of them are written in your answer, a good answer btw
That will work, but let's not encourage him to continue to write code that way. It's creating to much overhead (3 unnecessary operations that can be cut down easy) for a simple thing.
1

How to write the array:

$participants = array("sonia", "ice", "sasha","isa","omar","anwar");
or
$participants = array();
$participants[] = "sonia";
$participants[] = "ice";
and soo on

//make all participants name first letter uppercase
$participants = array_map(function($string) {return ucfirst($string);},$participants);

To get an random value from the array you have:

//get a random key
$random = array_rand($participants);
//get the value for the key
$winner = $participants[$random];
//unset the key and value
unset($participants[$random]);

OR

//this will shuffle values in the array on random positions
shuffle($participants);
//this return the last value from the array and deletes it from the array
$winner = array_pop($participants);


echo "The winner is $winner !\n";
echo "The losers are ".implode(', ',$participants)."\n";

Comments

0
<?php

// array

$winner = array();
array_push($winner,"sonia");
array_push($winner,"ice");
array_push($winner,"sasha");
array_push($winner,"isa");
array_push($winner,"omar");
array_push($winner,"anwar");

// random winner

$giocatori = count($winner);   
$random = rand(0,$giocatori -1);
echo "winner is".$winner[$random];
unset($winner[$random]);

// Sort the list

sort($winner);
echo print_r($winner) ;


echo "losers are ";
foreach($winner as $res) {
 echo $res. ' ';
}

?>

Output

winner is anwar                                                                                                                                                                                                                                        
Array                                                                                                                                                                                                                                                  
(                                                                                                                                                                                                                                                      
[0] => ice                                                                                                                                                                                                                                         
[1] => isa                                                                                                                                                                                                                                         
[2] => omar                                                                                                                                                                                                                                        
[3] => sasha                                                                                                                                                                                                                                       
[4] => sonia                                                                                                                                                                                                                                       
)                                                                                                                                                                                                                                                      
losers are ice isa omar sasha sonia

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.