0

I can explain what exactly I want.

Say I have five subjects (A, B, C, D, E), and each subject has an exam every day from Monday through Friday (i.e. 5 day buckets). So the $buckets array looks something like this:

$buckets = [
  [A2, B2, C2, D2, E2],
  [C3, A3, D3, B3, E3],
  [B4, D4, C4, E4, A4],
  [E5, B5, C5, A5, D5],
  [A6, B6, D6, C6, E6],
];

After separating by subject, I end up with:

$subjects = [
  'A' => [A2, A3, A4, A5, A6],
  'B' => [B2, B3, B4, B5, B6],
  'C' => [C2, C3, C4, C5, C6],
  'D' => [D2, D3, D4, D5, D6],
  'E' => [E2, E3, E4, E5, E6],
];

Now what i want is, from above to arrays, i want to flatten like this:

$buffers = [
  [A2, B2, C2, D2, E2],
  [A3, B3, C3, D3, E3],
  [A4, B4, C4, D4, E4],
  [A5, B5, C5, D5, E5],
  [A6, B6, C6, D6, E6],
]

Then if I flatten that and pick the first five, I end up with only the exams from Monday and none from the other days.

The desired output I want is, should cover multiple days and multiple subjects and uniquely; in this case, one possible selection could be [B2, D3, A4, E5, C6] (i.e. one from each day, different subject at random each day).

Any suggestion on how can I achieve that? Basically i want to do two things here, how to generate $buffer array and how to get desired output from it.

Thank you!

Mali C.
  • 11
  • 4

2 Answers2

0

Start from your final buffers, I would generate an array of random numbers using range and shuffle to extract the desired output:

$buffers = [
  ["A2", "B2", "C2", "D2", "E2"],
  ["A3", "B3", "C3", "D3", "E3"],
  ["A4", "B4", "C4", "D4", "E4"],
  ["A5", "B5", "C5", "D5", "E5"],
  ["A6", "B6", "C6", "D6", "E6"],
];


$final = [];
$values = range(0, 4);
shuffle($values);

$count = 0;
foreach ($buffers as $line)
   $final[] = $line[$values[$count++]];

print(implode(", ", $final));
print("\n\n");

Some outputs:

E2, D3, A4, C5, B6
B2, C3, E4, A5, D6
E2, B3, C4, D5, A6
E2, D3, C4, A5, B6
D2, E3, C4, B5, A6
B2, C3, D4, A5, E6
C2, E3, A4, B5, D6
D2, B3, A4, E5, C6
B2, A3, C4, D5, E6
A2, E3, D4, C5, B6
B2, D3, A4, E5, C6
E2, C3, A4, B5, D6
D P
  • 41
  • 4
  • Hello DP, Thanks for your answer. But i am sorry for the confusion, i also don't have $buffer array, or how should i generate that? With the shuffle you mention, i will not receive unique results. I see duplicate in your output. Can you please suggest random yet unique output? Thanks again. – Mali C. Jul 07 '21 at 04:25
0

I thought you already have codes to convert $buckets to $buffers. You can try these:

$buckets = [
  ["A2","B2","C2","D2","E2"],
  ["C3","A3","D3","B3","E3"],
  ["B4","D4","C4","E4","A4"],
  ["E5","B5","C5","A5","D5"],
  ["A6","B6","D6","C6","E6"],
];

$subjects = [];
foreach ($buckets as $items) {
    foreach ($items as $item) {
       $subject = substr($item, 0, 1);
       $subjects[$subject][] = $item;
    }
}
 
$buffers = [];
foreach ($subjects as $items) {
    foreach ($items as $item) {
       $number = substr($item, 1, 1);
       $buffers[$number][] = $item;
    }
}

How random is shuffle() ? You can try to improve the randomness by using an alternative method. I made some modification and called it shuffle2():

function shuffle2(&$array) {
    $len = count($array);
    $randomArray = [];
    while (count($randomArray) < $len) {
       $randomKey = mt_rand(0, count($array)-1);
       $randomArray[$randomKey] = $array[$randomKey];
    }
    $array =  array_values($randomArray);
 }

Append all the line outputs to an array, and use array_unique() to remove duplicates.

You may not see much improvement 'cause you have only 5 subjects to shuffle.

D P
  • 41
  • 4