【问题标题】:Shuffled array data repeats打乱的数组数据重复
【发布时间】:2014-12-08 12:11:19
【问题描述】:

我想洗牌数组值。我有像下面这样的多个清单,我收到下面代码的值。

$arrayplayer[0] = 10
$arrayplayer[1] = 1
$arrayplayer[2] = 17
$arrayplayer[3] = 9 

我想每次都像这样随机打乱数组的值。我该怎么做? 我尝试使用随机播放功能。似乎有些数据会重复。我只想随机重新排列数组。

$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
}
?>

【问题讨论】:

标签: php arrays shuffle


【解决方案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
)

【讨论】:

    【解决方案2】:

    你可以试试洗牌功能

    shuffle($arrayplayer);
    

    【讨论】:

      猜你喜欢
      • 2017-09-29
      • 2015-05-28
      • 2017-06-10
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 2018-06-04
      • 2021-09-25
      • 2014-04-14
      相关资源
      最近更新 更多