我想要改组数组值。我有多个清单,如下所示,我收到的代码如下。
$arrayplayer[0] = 10
$arrayplayer[1] = 1
$arrayplayer[2] = 17
$arrayplayer[3] = 9
我希望每次都随机随机调整数组的值。我该怎么做? 我尝试使用shuffle功能。似乎有些数据会重复出现。我只想随机重新排序数组。
$arrayplayer[0] = 17
$arrayplayer[1] = 9
$arrayplayer[2] = 10
$arrayplayer[3] = 1
我这样用:
foreach($_POST['chkplayer'] as $selected)
{
//echo $selected."</br>";
$arrayplayer[$index] = $selected;
shuffle($arrayplayer);
echo "arry[".$index."]==>".$arrayplayer[$index]. "<br>" ;
$index = $index +1 ;
}
我发现它复制了数组中的数据。
<html>
<head>
<meta http-equiv="Content-Language" content="th">
<meta http-equiv="content-Type" content="text/html; charset=window-874">
<meta http-equiv="content-Type" content="text/html; charset=tis-620">
<title>Select Player</title>
</head>
<body>
<?php
function shuffle_assoc(&$array) {
$keys = array_keys($array);
shuffle($keys);
foreach($keys as $key) {
$new[$key] = $array[$key];
}
$array = $new;
return true;
}
$id = $_GET["thisid"];
if(isset($_POST['submit']))
{
$amountteam = $_POST['perteam'];;
//echo "amount team=>".$amountteam;
echo "<h2>welcome to soccer random team</h2>";
$checked_arr = $_POST['chkplayer'];
$count = count($checked_arr);
$team1 = floor($count/$amountteam);
$team2 = $count%$amountteam;
echo "จำนวนคนที่มาเตะบอล".$count."<br>";
echo "แบ่งทีมได้ = ".$team1."ทีม";
echo "เศษ = ".$team2."คน<br>";
//put checkbox into array1 and array 2 is tricker"
$index = 0;
$indey = 0;
/*2dimension array
foreach($_POST['chkplayer'] as $selected)
{
//echo $selected."</br>";
for($indey =0 ; $indey<2 ; $indey++ )
{
if($indey ==0)
{
$arrayplayer[$index][$indey] = $selected;
}
else
{
$arrayplayer[$index][$indey] = -100 ;
}
echo "arry[".$index."][".$indey."]==>".$arrayplayer[$index][$indey]. "<br>" ;
}
$index = $index +1 ;
}
*/
foreach($_POST['chkplayer'] as $selected)
{
//echo $selected."</br>";
$arrayplayer[$index] = $selected;
shuffle($arrayplayer);
echo "arry[".$index."]==>".$arrayplayer[$index]. "<br>" ;
$index = $index +1 ;
}
$index = 0;
$indey = 0;
$total = 0;
//seperate team
echo"-------------TEAM -------------<br>";
for($index =0 ; $index<$team1 ; $index++ ) // 2 team
{
while(($indey<$amountteam)&&($total<$count)):
$finalteam[$index][$indey] = $arrayplayer[$total];
//$finalteam[$index][$indey] = $rand_array[$total];
echo "finalteam[".$index."][".$indey."]==>".$finalteam[$index][$indey]. "<br>" ;
$total++;
$indey++;
endwhile;
echo "================= <br>";
$indey =0 ;
}
}else{
?>
<form name="test" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<input type="checkbox" name="chkplayer[]" value="หง่าว">หง่าว<br>
<input type="checkbox" name="chkplayer[]" value="เต่า">เต่า<br>
<input type="checkbox" name="chkplayer[]" value="วิน">วิน<br>
<input type="checkbox" name="chkplayer[]" value="ไว">ไว<br>
<input type="checkbox" name="chkplayer[]" value="เก้อ">เก้อ<br>
<input type="checkbox" name="chkplayer[]" value="เสริม">เสริม<br>
<input type="checkbox" name="chkplayer[]" value="กอลฟ">กอลฟ<br>
<input type="checkbox" name="chkplayer[]" value="แบค">แบค<br>
<input type="checkbox" name="chkplayer[]" value="บัง">บัง<br>
<input type="checkbox" name="chkplayer[]" value="หมิน">หมิน<br>
<input type="checkbox" name="chkplayer[]" value="เอก">เอก<br>
<input type="checkbox" name="chkplayer[]" value="aaa">aaa<br>
<input type="checkbox" name="chkplayer[]" value="bbb">bbb<br>
<input type="checkbox" name="chkplayer[]" value="ccc">ccc<br>
<input type="checkbox" name="chkplayer[]" value="ddd">ddd<br>
<input type="checkbox" name="chkplayer[]" value="eee">eee<br>
<input type="checkbox" name="chkplayer[]" value="fff">fff<br>
<input type="checkbox" name="chkplayer[]" value="ggg">ggg<br><br>
<select name = "perteam">
<option value = "5">5คนต่อทีม</option>
<option value = "6">6คนต่อทีม</option>
<option value = "7">7คนต่อทีม</option>
</select>
<br><br>
<input type="submit" name="submit" value="ส่งข้อมูล"></td></tr>
<input name="clear" type="reset" value="เคลียร์">
</form>
<?php
}
?>
答案 0 :(得分:-1)
以下是使用PHP shuffle()
函数的示例。
$array = array(1,2,3,4,5);
echo "<pre>";
print_r($array);
echo "</pre>";
shuffle($array);
echo "<pre>";
print_r($array);
echo "</pre>";
输出:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Array
(
[0] => 4
[1] => 5
[2] => 3
[3] => 1
[4] => 2
)
答案 1 :(得分:-2)
您可以尝试随机播放功能
shuffle($arrayplayer);