What is a way to optimize the selection of two different images from an array using array_rand in PHP?

When using array_rand in PHP to select two different images from an array, we need to ensure that the two images selected are not the same. One way to optimize this selection is to shuffle the array first and then select the first two elements. This way, we can guarantee that the two images will be different.

// Shuffle the array to randomize the order
shuffle($images);

// Select the first two elements as the chosen images
$selectedImages = array_slice($images, 0, 2);

// Output the selected images
echo $selectedImages[0];
echo $selectedImages[1];