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

Unset rand() output from array in php

我想从数组中删除 $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);

您必须在 $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);

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

您可以使用 array_search function and then use unset 删除该值。

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