2016-09-29 144 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";