get random value from a PHP array, but make it unique

后端 未结 9 1127
挽巷
挽巷 2020-12-16 19:44

I want to select a random value from a array, but keep it unique as long as possible.

For example if I\'m selecting a value 4 times from a array of 4 elements, the s

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-16 20:12

    http://codepad.org/sBMEsXJ1

    = $numRandoms) {
    
            while (count($final) < $numRandoms) {
    
                $random = $array[rand(0, $count - 1)];
    
                if (!in_array($random, $final)) {
    
                    array_push($final, $random);
                }
            }
        }
    
        var_dump($final);
    
    ?>
    

提交回复
热议问题