What are the best practices for selecting and removing random elements from an array in PHP to ensure logical coherence, particularly in puzzle generation algorithms?

When selecting and removing random elements from an array in PHP for puzzle generation algorithms, it's important to ensure that the logic of the puzzle remains intact. One way to do this is by using the `array_rand()` function to select random elements and `unset()` to remove them without disrupting the order of the remaining elements.

// Original array
$array = [1, 2, 3, 4, 5];

// Select a random element
$randomKey = array_rand($array);
$randomElement = $array[$randomKey];

// Remove the selected element
unset($array[$randomKey]);

// Re-index the array
$array = array_values($array);

// Output the random element and updated array
echo "Random Element: " . $randomElement . "\n";
print_r($array);