a0101 Posted June 26, 2012 Share Posted June 26, 2012 Hi, I need to generate a random array that stores various values. For example, <form action="testscript.php" method="POST"> Value1:<input type = "text" name="value1"> Value2:<input type = "text" name="value2"> <?php $valueone = $_POST['value1']; $valuetwo = $_POST['value2']; $number = random(1,10); //random value for array $array[$number] = array($valueone,$valuetwo); //each array number is unique ?> <input type="submit" name="submit"> </form> However, I need to make it such that if an array already has values stored in it, it moves on to another random array until it finds an empty one. Also, since I would need to validate the form, I will require the random array number to remain the same upon submission of the form. I've tried using a for loop: for($i=0; $i<1000; $i++){ if(isset($array[$number])){ //if the array is set, since $number is unique, generate new array by randoming another number $number = random(1,10); //random another value for array } $i++; } but it doesn't seem to work the way I want it to. Quote Link to comment https://forums.phpfreaks.com/topic/264788-storing-html-values-in-array/ Share on other sites More sharing options...
requinix Posted June 26, 2012 Share Posted June 26, 2012 If you have an array already, shuffle it and pull the first N items from it. Quote Link to comment https://forums.phpfreaks.com/topic/264788-storing-html-values-in-array/#findComment-1357017 Share on other sites More sharing options...
a0101 Posted June 27, 2012 Author Share Posted June 27, 2012 I didn't use shuffle as I want my array to store values from the 100th position. So basically, array[0] -> array[99] are empty. I have a problem which is I am unable to capture the random number upon validation of the form. When I generate a random number say: $rand = rand(100,999); I want to be able to echo the same random number, instead when I submit the form, my code generates another random number. I can't use another variable to store the random number as again $storage = $rand; when I submit the form, $storage will also be the new random number instead of the old one. Is there any way to capture the random number? Quote Link to comment https://forums.phpfreaks.com/topic/264788-storing-html-values-in-array/#findComment-1357426 Share on other sites More sharing options...
TFT2012 Posted June 27, 2012 Share Posted June 27, 2012 You may need an additional array to store the generated random number, like $array1 = array(); $array1[] = random(n, m); The last element in the $array1 will be the number you generate most recently. Quote Link to comment https://forums.phpfreaks.com/topic/264788-storing-html-values-in-array/#findComment-1357473 Share on other sites More sharing options...
Barand Posted June 27, 2012 Share Posted June 27, 2012 if you are looking for an empty array index do { $i = rand(1,10); } while (isset($arr[$i])); Quote Link to comment https://forums.phpfreaks.com/topic/264788-storing-html-values-in-array/#findComment-1357541 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.