-1

I want to shuffle array values. I have multi checklist like below and I receive the value with code below.

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

I want to random shuffle the values of the array like this everytime. How can I do it? I try to use shuffle function. It seems like some data will repeat. I only want to randomly reorder the array.

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

I use it like this:

foreach($_POST['chkplayer'] as $selected)
{   
    //echo $selected."</br>";   
    $arrayplayer[$index] =  $selected;
    shuffle($arrayplayer);
    echo "arry[".$index."]==>".$arrayplayer[$index]. "<br>" ;
    $index = $index +1 ;
}

I found that it duplicates data in the array.


<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
}
?>
Taryn
  • 242,637
  • 56
  • 362
  • 405
  • 1
    possible duplicate of [Shuffle an array in PHP](http://stackoverflow.com/questions/5334215/shuffle-an-array-in-php) – Naruto Dec 08 '14 at 12:12
  • use some swap operations using an intermediate variables in a for loop :D – Vaisakh Pc Dec 08 '14 at 12:13
  • using google for 10 seconds would have taken you straight to the php documents for the function called shuffle() http://php.net/manual/en/function.shuffle.php – Dave Dec 08 '14 at 12:13
  • check this link http://stackoverflow.com/questions/9556847/php-array-shuffle-keeping-unique – manuyd Dec 08 '14 at 12:22
  • [DEMO using shuffle()](http://ideone.com/2tVrjz) and there is no duplication of data – Mark Baker Dec 08 '14 at 12:25

2 Answers2

-1

Here is an example using PHP's shuffle() function.

$array = array(1,2,3,4,5);
echo "<pre>";
print_r($array);
echo "</pre>";
shuffle($array);
echo "<pre>";
print_r($array);
echo "</pre>";

Outputs:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
Array
(
    [0] => 4
    [1] => 5
    [2] => 3
    [3] => 1
    [4] => 2
)
Arko Elsenaar
  • 1,689
  • 3
  • 18
  • 33
-2

You could try the shuffle function

shuffle($arrayplayer);
boeing
  • 302
  • 7
  • 19