基于随机变量的php变量

时间:2016-09-29 11:27:46

标签: php random

我是php的新手,我写了一个代码,从array中随机获胜并显示它,然后我想尝试显示没有获胜者的所有名字#39; s,但我如何使用生成的随机数unset获胜者?我设法unset一个随机名称但与获胜者不一样......

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

?>

3 个答案:

答案 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);

答案 1 :(得分:1)

如何编写数组:

<body ng-app="BuildApp">
<h1>Branch</h1>
<div  ng-controller="BranchController">
    <div class="dropdown">
        <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> {{selectedBranch}}
            <span class="caret"></span></button>
        <ul class="dropdown-menu" aria-labelledby="dropdownMenu1" >
            <li ng-repeat="branch in branchs"><branch ng-click = "dropboxitemselectedbranch(branch.name)"> {{branch.name}} </branch></li>
        </ul>
    </div>
</div>

<h1>Kit</h1>

<div  ng-controller="KitController">
    <div class="dropdown">
        <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> {{selectedKit}}
            <span class="caret"></span></button>
        <ul class="dropdown-menu" aria-labelledby="dropdownMenu2" >
            <li ng-repeat="kit in kits"><kit ng-click = "dropboxitemselectedkit(kit.name)">{{kit.name}}</kit></li>
        </ul>
    </div>
</div>

</body>

要从您拥有的数组中获取随机值:

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

OR

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

答案 2 :(得分: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