dousong4777 2014-03-24 16:23
浏览 35
已采纳

将它们之间的所有元素配对而不重复

At this point my script is working but not fully. I managed to pair elements without duplicates, but i cant seem to find any way to repeat the loop so i can get all the possible results. From 20 possible results i get only 16 to 19 results. Any help would be much appreciated. Here's the long code + output

$studentList = array
    (
    array('up1','up2','up3','up4','up5','up6', 'up7', 'up8' , 'up9' , 'up10'),
    array('up1','up2','up3','up4','up5','up6', 'up7', 'up8' , 'up9' , 'up10'),
    array('up1','up2','up3','up4','up5','up6', 'up7', 'up8' , 'up9' , 'up10'),
    );
//count how many times the user wants to pair students up
$AC = count($studentList);

//Take away one from the count due to the first aray used for setting up the pairs
$AC--;

//count how may users need to be paired
$c = count($studentList[0]);
$totalCount = $AC * $c;
echo $totalCount."<= total count<br>"; 

for ($b = 1; $b <= $AC; $b++)
{
    shuffle($studentList[$b]);
    print_R ($studentList[$b]);
    echo"<br>";
}
$z = 0;
$r = 1;
$flagReset = 0;
//this will look to make sure the results are random
for ($i = 0; $i < $totalCount; $i++)
{   
    if ($studentList[0][$z] == $studentList[$r][$z]){}
    else
    {
        $randomArray[$i] = $studentList[0][$z] ."/".$studentList[$r][$z];
    }
   //echo $z."<= z count|";
   if ($i == 0)
   {
       echo "i am the first $z / $c / $r <br>";
   }       

   if ($z == $c - 1 )
    {
        if ($i < $totalCount -1 )
        {
            $r++;
            $z= 0;
            $flagReset = 1;
        }
        else 
        {
            $flagReset = 1;
        }
    }

    if ($flagReset == 1)
    {
        $flagReset = 0;
    }

    else
    {
        $z++;
    }

    if ($i == 19)
    {
        echo "i am the last $z / $c / $r <br>";
    }       

    array_unique($randomArray, SORT_REGULAR);
    $rand = count($randomArray);
} 
echo "<br>";
array_unique($randomArray, SORT_REGULAR);
$rand = count($randomArray);
print_r($randomArray);
print $rand;

Output:

20<= total count
Array ( [0] => up8 [1] => up9 [2] => up2 [3] => up4 [4] => up5 
        [5] => up6 [6] => up3 [7]      => up10 [8] => up1 [9] => up7 ) 
Array ( [0] => up4 [1] => up5 [2] => up8 [3] => up7 [4] => up6 [5] => up1 
        [6] => up2 [7]     => up9 [8] => up10 [9] => up3 ) 
i am the first 0 / 10 / 1 
i am the last 9 / 10 / 2 

Array ( [0] => up1/up8 [1] => up2/up9 [2] => up3/up2 [6] => up7/up3 
        [7] => up8/up10 [8] => up9/up1 [9] => up10/up7 [10] => up1/up4 
        [11] => up2/up5 [12] => up3/up8 [13] => up4/up7 [14] => up5/up6 
        [15] => up6/up1 [16] => up7/up2 [17] => up8/up9 [18] => up9/up10 
        [19] => up10/up3 ) 17
  • 写回答

1条回答 默认 最新

  • dongpai2468 2014-03-24 17:22
    关注

    Not really anymore a temporary answer, just an answer

    From what I've understood, you're trying to find all the possible combinations from two shuffled arrays of the array $studentlist.

    I've tried reading over and over the code you've written, but I'm not really understanding why you're using so many for loops and flag.

    What I've tried to do is this:

        <?php
            // stackoverflow test area
            $studentList = array
            (
            array('up1','up2','up3','up4','up5','up6', 'up7', 'up8' , 'up9' , 'up10'),
            array('up1','up2','up3','up4','up5','up6', 'up7', 'up8' , 'up9' , 'up10'),
            array('up1','up2','up3','up4','up5','up6', 'up7', 'up8' , 'up9' , 'up10')
            );
    //count how many times the user wants to pair students up
        $AC = count($studentList);
    
        //Take away one from the count due to the first aray used for setting up the pairs
        $AC--;
    
        //count how may users need to be paired
        $c = count($studentList[0]);
        $totalCount = $AC * $c;
        echo $totalCount."<= total count<br>"; 
    
        for ($b = 1; $b <= $AC; $b++)
        {
            shuffle($studentList[$b]);
            print_R ($studentList[$b]);
            echo"<br>";
        }
        $randomArray = array();
        //this will look to make sure the results are random
        foreach ($studentList[0] as $value) {
            foreach ($studentList[1] as $second_value) {
                if ($value !== $second_value) {
                    if (!in_array("{$value}/{$second_value}",$randomArray) and !in_array("{$second_value}/{$value}",$randomArray)) {
                        $randomArray[] = "{$value}/{$second_value}";
                    }
                }
            }
        }
        array_unique($randomArray, SORT_REGULAR);
        $rand = count($randomArray);
        print_r($randomArray);
        print $rand;
    ?>
    

    The big deal is here:

    foreach ($studentList[0] as $value) {
            foreach ($studentList[1] as $second_value) {
                if ($value !== $second_value) {
                    if (!in_array("{$value}/{$second_value}",$randomArray) and !in_array("{$second_value}/{$value}",$randomArray)) {
                        $randomArray[] = "{$value}/{$second_value}";
                    }
                }
            }
        }
    

    I'm not understing why you're doing it with a regular for loop.

    The goal is basicly finding all the possible combinations, right? so why not looping through both the arrays and just check if:

    1. the values are identical ( ignore in this case ).
    2. the values are different.

    In the second case, there are two possibilities:

    1. the value x1/x2 or x2/x1 already exists in the array (ignore).
    2. the value x1/x2 or x2/x1 doesn't yet exist. Push it.

    And the result is this:

    20<= total count
    Array ( [0] => up4 [1] => up10 [2] => up9 [3] => up6 [4] => up5 [5] => up7 [6] => up3 [7] => up8 [8] => up2 [9] => up1 ) 
    
    Array ( [0] => up10 [1] => up8 [2] => up6 [3] => up5 [4] => up7 [5] => up4 [6] => up1 [7] => up2 [8] => up3 [9] => up9 ) 
    
    Array ( [0] => up1/up4 [1] => up1/up10 [2] => up1/up9 [3] => up1/up6 [4] => up1/up5 [5] => up1/up7 [6] => up1/up3 [7] => up1/up8 [8] => up1/up2 [9] => up2/up4 [10] => up2/up10 [11] => up2/up9 [12] => up2/up6 [13] => up2/up5 [14] => up2/up7 [15] => up2/up3 [16] => up2/up8 [17] => up3/up4 [18] => up3/up10 [19] => up3/up9 [20] => up3/up6 [21] => up3/up5 [22] => up3/up7 [23] => up3/up8 [24] => up4/up10 [25] => up4/up9 [26] => up4/up6 [27] => up4/up5 [28] => up4/up7 [29] => up4/up8 [30] => up5/up10 [31] => up5/up9 [32] => up5/up6 [33] => up5/up7 [34] => up5/up8 [35] => up6/up10 [36] => up6/up9 [37] => up6/up7 [38] => up6/up8 [39] => up7/up10 [40] => up7/up9 [41] => up7/up8 [42] => up8/up10 [43] => up8/up9 [44] => up9/up10 ) 45
    

    Am I going wrong somewhere or is this what you're looking for?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理