Get random value from PHP array, but make it unique

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

For example, if I select a value 4 times from an array of 4 elements, the selected value should be random, but different each time.

If I select it 10 times from the same array of 4 elements, then obviously some values โ€‹โ€‹will be duplicated.

I have it right now, but I still get duplicate values, even if the loop runs 4 times:

$arr = $arr_history = ('abc', 'def', 'xyz', 'qqq'); for($i = 1; $i < 5; $i++){ if(empty($arr_history)) $arr_history = $arr; $selected = $arr_history[array_rand($arr_history, 1)]; unset($arr_history[$selected]); // do something with $selected here... } 
+7
source share
8 answers

You are almost right. The problem was the unset($arr_history[$selected]); line unset($arr_history[$selected]); . The value of $selected not a key, but actually a value, so unset will not work.

To keep it the same as you have there:

 <?php $arr = $arr_history = array('abc', 'def', 'xyz', 'qqq'); for ( $i = 1; $i < 10; $i++ ) { // If the history array is empty, re-populate it. if ( empty($arr_history) ) $arr_history = $arr; // Select a random key. $key = array_rand($arr_history, 1); // Save the record in $selected. $selected = $arr_history[$key]; // Remove the key/pair from the array. unset($arr_history[$key]); // Echo the selected value. echo $selected . PHP_EOL; } 

Or an example with a few smaller lines:

 <?php $arr = $arr_history = array('abc', 'def', 'xyz', 'qqq'); for ( $i = 1; $i < 10; $i++ ) { // If the history array is empty, re-populate it. if ( empty($arr_history) ) $arr_history = $arr; // Randomize the array. array_rand($arr_history); // Select the last value from the array. $selected = array_pop($arr_history); // Echo the selected value. echo $selected . PHP_EOL; } 
+8
source

How about shuffling an array and dropdowns.

When pop returns null , reset the array.

 $orig = array(..); $temp = $orig; shuffle( $temp ); function getNextValue() { global $orig; global $temp; $val = array_pop( $temp ); if (is_null($val)) { $temp = $orig; shuffle( $temp ); $val = getNextValue(); } return $val; } 

Of course, you'll want to encapsulate it better, and better check out other things like that.

+7
source

Php has a built-in shuffle function that you can use to randomly arrange elements in an array. So what about this?

 $arr = ('abc', 'def', 'xyz', 'qqq'); $random = shuffle($arr); foreach($random as $number) { echo $number; } 
+3
source

key! = value, use this:

 $index = array_rand($arr_history, 1); $selected = $arr_history[$index]; unset($arr_history[$index]); 
+2
source

http://codepad.org/sBMEsXJ1

 <?php $array = array('abc', 'def', 'xyz', 'qqq'); $numRandoms = 3; $final = array(); $count = count($array); if ($count >= $numRandoms) { while (count($final) < $numRandoms) { $random = $array[rand(0, $count - 1)]; if (!in_array($random, $final)) { array_push($final, $random); } } } var_dump($final); ?> 
+2
source

I did this to create a random 8-digit password for users:

 $characters = array( "A","B","C","D","E","F","G","H","J","K","L","M", "N","P","Q","R","S","T","U","V","W","X","Y","Z", "a","b","c","d","e","f","g","h","i","j","k","m", "n","p","q","r","s","t","u","v","w","x","y","z", "1","2","3","4","5","6","7","8","9"); for( $i=0;$i<=8;++$i ){ shuffle( $characters ); $new_pass .= $characters[0]; } 
+1
source

If you do not need any specific values โ€‹โ€‹in the array, you can try to implement the Linear Congruential Generator to generate all the values โ€‹โ€‹in the array.

LCG execution

Wikipedia lists some values โ€‹โ€‹that you can use and the rules for selecting values โ€‹โ€‹for the LCG algorithm, since the LCG algorithm is deterministic, it is guaranteed not to repeat any values โ€‹โ€‹until the period period.

After filling the array with these unique numbers, you can simply get the numbers in the 1 by 1 array in order.

+1
source

Easy and clean:

 $colors = array('blue', 'green', 'orange'); $history = $colors; function getColor($colors, &$history){ if(count($history)==0) $history = $colors; return array_pop( $history ); } echo getColor($colors, $history); 
-one
source

All Articles