How can you prevent duplicate selections when randomly choosing checkboxes in PHP?

When randomly choosing checkboxes in PHP, one way to prevent duplicate selections is to keep track of which checkboxes have already been selected. You can achieve this by using an array to store the selected checkboxes and checking against it before making a selection.

// Initialize an empty array to store selected checkboxes
$selectedCheckboxes = [];

// Loop through the checkboxes and randomly select one that hasn't been selected yet
foreach ($checkboxes as $checkbox) {
    do {
        $randomIndex = array_rand($checkboxes);
    } while (in_array($randomIndex, $selectedCheckboxes));

    // Mark the selected checkbox as selected
    $selectedCheckboxes[] = $randomIndex;

    // Perform actions on the selected checkbox
    $selectedCheckbox = $checkboxes[$randomIndex];
    // Do something with $selectedCheckbox
}