How do I select 10 random things from a list in PHP?
You can select one or more random items from an array using array_rand()
function.
php get two different random array elements
You could always remove the element that you selected the first time round, then you wouldn't pick it again. If you don't want to modify the array create a copy.
for ($i=0; $i<2; $i++) {
$random = array_rand($my_array); # one random array element number
$get_it = $my_array[$random]; # get the letter from the array
echo $get_it;
unset($my_array[$random]);
}
How do I select random values from an array in PHP?
$result = array();
foreach( array_rand($my_array, 8) as $k ) {
$result[] = $my_array[$k];
}
Get random item from array
echo $items[array_rand($items)];
array_rand()
Get five unique random PHP values selected from an array and put them in separate variables
You can shuffle
the array and use list
to assign the values
$arr = array("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg");
shuffle( $arr );
list($one, $two, $three, $four, $five) = $arr;
Doc: shuffle(), list()
How to get random value out of an array?
You can also do just:
$k = array_rand($array);
$v = $array[$k];
This is the way to do it when you have an associative array.
Randomly pick element in array then remove from the array
Shuffle the array in random order, and just pop the last element off.
$array = [...];
shuffle($array);
while($element = array_pop($array)){
echo 'Random element:' . $element;
}
Select Random Unique String From List in PHP
You could try copying the array, then shuffling the copy and finally popping the results.
For example:
$array = array('blue','green','yellow','purple');
$copy = $array;
shuffle($copy);
while(!empty($copy)){
echo array_pop($copy);
}
Choose 3 different random values from an array
Shamelessly stolen from the PHP manual:
<?php
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
?>
http://us2.php.net/array_rand
Note that, as of PHP 5.2.10, you may want to shuffle (randomize) the keys that are returned via shuffle($rand_keys)
, otherwise they will always be in order (smallest index first). That is, in the above example, you could get "Neo, Trinity" but never "Trinity, Neo."
If the order of the random elements is not important, then the above code is sufficient.
Related Topics
How to Get Woocommerce Variation Id
Using PHP Replace Regex with Regex
What Is the Use of Strict in Laravel Config/Database
Output (Echo/Print) Everything from a PHP Array
How to Get Average of Column Values in Laravel
Laravel 5 Carbon Global Locale
Redirect to New Page W/ Post Data (Php/Zend)
Add X Days to Date, Excluding Weekends
Indented List to Multidimensional Array
PHP - Make Multi-Dimensional Associative Array from a Delimited String
How to Solve This "Warning: MySQLi_Connect(): (Hy000/1049): Unknown Database" Problem
Trouble Passing Variables to PHP Script on Image Upload
Why Can't I Pass User Sessions Between Subdomains
Passing a Boolean Value from Checkbox in Laravel Form
Mongodb Aggregate Query Using PHP Driver
Set Quantity Minimum, Maximum and Step at Product Level in Woocommerce