2016-09-29 141 views
1

我是的新手,我寫了一個代碼,需要從array中隨機獲得一個代碼並顯示它,然後我想嘗試顯示沒有獲勝者的所有名字,但是如何我可以通過使用生成的隨機數獲得勝者unset嗎?我到unset管理的隨機名稱,但不一樣的贏家...基於一個隨機變量的php變量

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

,並解釋爲什麼你的代碼沒有工作:你有從數組隨機密鑰和接下來的操作就是取消它,這意味着你刪除鍵和值從數組中,稍後您想要使用該鍵來獲取值並在刪除之後顯示它。 –

回答

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

爲什麼要在你已經知道密鑰的數組上做array_diff?另外PHP有一個函數array_rand(); –

+0

我保留了他寫的一些東西,但還有很多其他的方法可以做到這一點,其中一些是寫在你的答案,一個很好的答案btw –

+1

這將工作,但我們不要鼓勵他繼續以這種方式編寫代碼。它創造了很多開銷(3個不必要的操作,可以簡單地減少),這是一件簡單的事情。 –

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

?> 

輸出

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

怎麼寫數組:

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

要從數組中獲得隨機值,您需要:

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