Thanks for the clarification. I now understand that you need all 5 profiles for Question Qa. And then again in Question Qb, but you need to ensure that the profile is always a different one.

So, what you actually need is different rotations of the profile order (there are 5! = 120 possible rotations), but the number of possible orders is limited in each iteration, i.e., you have only 4! = 24 possible rotations for Qb (given the Qa order) and only 3! = 6 for Qc.

The only option I see here, is to solve this programatically. That is: Do the first rotation (Qa), then define what profiles are left for each group for Qb, and so on. The challenge is that you have two restrictions at once/at cross (unique per profile, and unique per question), and while I am sure that there is an efficient algorithm for that, I do not know that one currently.

What I can offer is solution that will use 120 of the possible 120 * 24 * 6 * 2 = 34.560 possible combinations.

```
// Define the profiles
$profiles = [1,2,3,4,5];
// Build a matrix with a valid solution
$matrix = [];
for ($x=0; $x<5; $x++) {
$matrix[$x] = [];
for ($y=0; $y<5; $y++) {
$matrix[$x][$y] = ($x + $y) % 5;
}
}
// Now shuffle rows
$order = [0,1,2,3,4];
shuffle($order);
// Resort matrix (1)
$matrix2 = [];
for ($x=0; $x<5; $x++) {
$matrix2[$x] = [];
}
foreach ($order as $old => $nw) {
for ($x=0; $x<5; $x++) {
$matrix2[$x][$old] = $matrix[$x][$nw];
}
}
$matrix = $matrix2;
// Now shuffle columns
$order = [0,1,2,3,4];
shuffle($order);
// Resort matrix (2)
$matrix2 = [];
foreach ($order as $old => $nw) {
$matrix2[$nw] = $matrix[$old];
}
$matrix = array_values($matrix2);
debug($matrix);
```

Store that matrix in internal variables, and use them to select the profiles in question Qa (that would be `$matrix[0]`

), Qb (that would be `$matrix[1]`

), etc.

If you're not sure about the brackets, read the manuals on Arrays, please.