2016-09-29 5 views
1

Ich bin neu in php und ich schrieb einen Code, der einen zufälligen Gewinner aus einem array und zeigt es an, dann wollte ich versuchen, alle Namen ohne die Gewinner anzuzeigen, aber wie kann ich unset den Gewinner mit der Zufallszahl generieren? Ich schaffte es unset einen zufälligen Namen, aber nicht das gleiche wie die Gewinner ...PHP Variable basierend auf einer Zufallsvariablen

<?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"; 

?> 
+0

Und zu erklären, warum der Code nicht funktioniert: Sie bekam einen zufälligen Schlüssel aus dem Array und die nächste Operation ist es unscharf zu schalten, das bedeutet, dass Sie den Schlüssel und Wert gelöscht aus dem Array möchten Sie später den Schlüssel verwenden, um den Wert zu erhalten und anzuzeigen, nachdem Sie es gelöscht haben. –

Antwort

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); 
+0

Warum würden Sie array_diff in einem Array machen, wo Sie den Schlüssel bereits kennen ??? Auch PHP hat eine Funktion array_rand(); –

+0

Ich habe einige Dinge, die er schrieb, aber es gibt viele andere Möglichkeiten, dies zu tun, einige von ihnen sind in Ihrer Antwort geschrieben, eine gute Antwort btw –

+1

Das wird funktionieren, aber lasst uns ihn nicht ermutigen, weiterhin Code auf diese Weise zu schreiben. Es schafft zu viel Aufwand (3 unnötige Operationen, die einfach geschnitten werden können) für eine einfache Sache. –

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. ' '; 
} 

?> 

Ausgabe

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

Wie das Array schreiben:

$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); 

Um einen zufälligen Wert aus dem Array zu erhalten, haben Sie:

//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]); 

ODER

//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"; 
Verwandte Themen