php - Pair all elements between them without duplicates -
at point script working not fully. managed pair elements without duplicates, cant seem find way repeat loop can possible results. 20 possible results 16 19 results. appreciated. here's 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 user wants pair students $ac = count($studentlist); //take away 1 count due first aray used setting pairs $ac--; //count how may users need paired $c = count($studentlist[0]); $totalcount = $ac * $c; echo $totalcount."<= total count<br>"; ($b = 1; $b <= $ac; $b++) { shuffle($studentlist[$b]); print_r ($studentlist[$b]); echo"<br>"; } $z = 0; $r = 1; $flagreset = 0; //this make sure results random ($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 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 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 ) first 0 / 10 / 1 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
not anymore temporary answer, answer
from i've understood, you're trying find possible combinations 2 shuffled arrays of array $studentlist.
i've tried reading on , on code you've written, i'm not understanding why you're using many loops , flag.
what i've tried 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 user wants pair students $ac = count($studentlist); //take away 1 count due first aray used setting pairs $ac--; //count how may users need paired $c = count($studentlist[0]); $totalcount = $ac * $c; echo $totalcount."<= total count<br>"; ($b = 1; $b <= $ac; $b++) { shuffle($studentlist[$b]); print_r ($studentlist[$b]); echo"<br>"; } $randomarray = array(); //this make sure results random foreach ($studentlist[0] $value) { foreach ($studentlist[1] $second_value) { if ($value !== $second_value) { if (!in_array("{$value}/{$second_value}",$randomarray) , !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 here:
foreach ($studentlist[0] $value) { foreach ($studentlist[1] $second_value) { if ($value !== $second_value) { if (!in_array("{$value}/{$second_value}",$randomarray) , !in_array("{$second_value}/{$value}",$randomarray)) { $randomarray[] = "{$value}/{$second_value}"; } } } }
i'm not understing why you're doing regular loop.
the goal basicly finding possible combinations, right? why not looping through both arrays , check if:
- the values identical ( ignore in case ).
- the values different.
in second case, there 2 possibilities:
- the value x1/x2 or x2/x1 exists in array (ignore).
- the value x1/x2 or x2/x1 doesn't yet exist. push it.
and result 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 going wrong somewhere or you're looking for?
Comments
Post a Comment