[help] php array, randomly combine output strings, refresh web page reassembly order, do not repeat

multiple keywords in an array are randomly combined for output. Refresh combination order is different
output string

$a=array ("red", "green", "blue", "yellow", "brown");
$random_keys=array_rand ($random_keys 4);
echo $a [$random_keys [0]]. "
";
echo $a [$random_keys [1]]. "
";
echo $a [$random_keys [2]];

this almost meets my requirements.
but the combined data will be repeated
should not be difficult, but I am a rookie.
give me a big brother to help

Php
Mar.12,2021

the easiest way

$a = ["red", "green", "blue", "yellow", "brown"];
shuffle($a);    // 
echo array_shift($a);    // array_pop
echo array_shift($a);
echo array_shift($a);

Note that this method changes the original array. If you don't want to change the original array, you need to add a judgment

.
$a = ["red", "green", "blue", "yellow", "brown"];
shuffle($a);
$has = [];    // 

// 
while (!in_array($a[0], $has) && count($has) <= 3) {
    $has[] = $a[0];
}

echo implode(',', $has); // ,

// 
shuffle($a);
// 
return implode('', array_slice($a, 3));
// :greenbrownred

feels that what you need is a shuffle algorithm, which is the shuffle function.

Menu