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

时间:2014-03-24 16:23:28

标签: php for-loop random

此时我的脚本正在运行但不完全。我设法配对没有重复的元素,但我似乎无法找到任何重复循环的方法,所以我可以获得所有可能的结果。从20个可能的结果我只得到16到19个结果。任何帮助将非常感激。 这是长码+输出

$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;

输出:

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 个答案:

答案 0 :(得分:1)

不再是临时答案,只是一个答案

根据我的理解,您正在尝试从阵列$ studentlist的两个混洗数组中找到所有可能的组合。

我已经尝试过一遍又一遍地阅读您编写的代码,但我并不理解您为什么要使用这么多for循环和旗帜。

我试图做的是:

    <?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;
?>

重要的是:

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}";
                }
            }
        }
    }

我没有说明你为什么要用常规for循环来做这件事。

目标是基本找到所有可能的组合,对吧?那么为什么不循环遍历两个数组,只检查是否:

  1. 值相同(在这种情况下忽略)。
  2. 值不同。
  3. 在第二种情况下,有两种可能性:

    1. 值x1 / x2或x2 / x1已存在于数组中(忽略)。
    2. 值x1 / x2或x2 / x1尚不存在。推它。
    3. 结果如下:

      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
      

      我在哪里出错或者这是你在寻找什么?