fork download
  1. <?php
  2. function getRandomResults(array $source, $quantity) {
  3. $keys = array_keys($source);
  4.  
  5. // how many elements are there in our source array
  6. $length = count($keys);
  7.  
  8. // where we store our result
  9. $result = [];
  10.  
  11. // iterate for x quantity
  12. for($c=0;$c < $quantity; $c++) {
  13. // add random result from source to result array.
  14. $result[] = $source[$keys[rand(0, $length-1)]];
  15. }
  16. return $result;
  17. }
  18. $res = getRandomResults([
  19. "10",
  20. "20",
  21. "30",
  22. 'bleh' => '40',
  23. ], 6);
  24. var_dump($res);
Success #stdin #stdout 0.02s 23504KB
stdin
Standard input is empty
stdout
array(6) {
  [0]=>
  string(2) "40"
  [1]=>
  string(2) "10"
  [2]=>
  string(2) "10"
  [3]=>
  string(2) "40"
  [4]=>
  string(2) "20"
  [5]=>
  string(2) "10"
}