从php中的数组取消设置rand()输出

时间:2015-02-13 09:13:28

标签: php arrays random

我想从数组中删除$win值并打印除赢家之外的所有名称。请查看以下代码。

<html>
<p>
<?php
// Create an array and push on the names
// of your closest family and friends
$array=array();
array_push( $array,"preet");
array_push($array,"limbu");
array_push($array,"nik");
array_push($array,"rohit");
array_push($array,"ravi");
// Sort the list
sort($array);
echo"all guys are ".$f=join(", ",$array);
echo"<br>Lets see who is winner</br> ";
$len=count($array);
// Randomly select a winner!
$win=strtoupper( $array[rand(0,$len-1)]);


// Print the winner's name in ALL CAPS
echo "$win";

//print name of all except to winner.but given below code is not working
unset($win);
print"<br> sory ".join(",",$array);

2 个答案:

答案 0 :(得分:-1)

您必须在$array

中取消设置获胜者的条目
<?php

// Create an array and push on the names
// of your closest family and friends
$array = array();
array_push($array, "preet");
array_push($array, "limbu");
array_push($array, "nik");
array_push($array, "rohit");
array_push($array, "ravi");
// Sort the list
sort($array);
echo"all guys are " . $f = join(", ", $array);
echo"<br>Lets see who is winner</br> ";
$len = count($array);
// Randomly select a winner!
$random = rand(0, $len - 1);
$win = strtoupper($array[$random]);


// Print the winner's name in ALL CAPS
echo $win;

//print name of all except to winner.but given below code is not working
unset($array[$random]);
print"<br> sory " . join(",", $array);

答案 1 :(得分:-1)

您需要知道数组中值的索引才能从数组中删除该项。

您可以使用array_search功能执行此操作,然后使用unset删除该值。

if(($key = array_search($win, $array)) !== false) {
    unset($array[$win]);
}