OM2 Posted May 18, 2012 Share Posted May 18, 2012 I want to create an array of 1 - X (X could be 10, 20, 30 or any number) Then I want to randomly choose a number from 1 - X I now want to remove the random choice - and then do the same again twice Question 1: How do I create the array in the first place? Answer: var x=[0,1,2,3,4,5,6,7,8,9,10]; I'd much prefer it if I could do something like this: var x=[0...10]; This would create the same, except, I don't have to type out all the numbers! And if I needed to increase the number in the array, I could simply just change the last number I've just made the syntax up I was hoping something similar shorthand existed Or do I have to do this by creating a loop? Question 2: how do I pop an item from the middle or somewhere other than the beginning or end? I've googled and all I can find is how to remove the first and last items! The way I see it: - Get a solution for popping from the middle of an array I'm sure I'll find a solution to this if I looked - but is it optimal to be doing it this way? - Instead, maybe I could fill my array in a random order in the first place - and then just pop at the beginning or end?? Which is the best solution? Any code to start me off would be great Thanks OM Quote Link to comment https://forums.phpfreaks.com/topic/262749-need-array-creation-and-popping-from-the-middle-advice/ Share on other sites More sharing options...
smoseley Posted May 19, 2012 Share Posted May 19, 2012 What you're looking for: // Add 40 items to your array $totalNums = 40; // or whatever $array = array(); for ($i = 1; $i <= $totalNumbs; $i++) $array[] = $i; // Echo them out in random order $count = count($array); while ($count > 0) { $i = rand(0,$count-1); echo $array[$i] unset($array[$i]) $count = count($array); } Quote Link to comment https://forums.phpfreaks.com/topic/262749-need-array-creation-and-popping-from-the-middle-advice/#findComment-1346710 Share on other sites More sharing options...
OM2 Posted May 19, 2012 Author Share Posted May 19, 2012 thanks for that - that's really helpful i have coded the following (before getting your reply about how best to fill up the array): var numberoffiles = [1,2,3,4,5,6,7,8,9,10]; var random_page_index = Math.ceil(Math.random()*numberoffiles.length) - 1; var random_page_selector = "random_html_code/random_" + numberoffiles[random_page_index] + ".html"; numberoffiles.splice(random_page_index, 1); // only removing one index, thus the 1 $("#random_load1").load ( random_page_selector, function() { $("#random_load1").trigger("create"); } ); random_page_index = Math.ceil(Math.random()*numberoffiles.length) - 1; random_page_selector = "random_html_code/random_" + numberoffiles[random_page_index] + ".html"; numberoffiles.splice(random_page_index, 1); // only removing one index, thus the 1 $("#random_load2").load ( random_page_selector, function() { $("#random_load2").trigger("create"); } ); random_page_index = Math.ceil(Math.random()*numberoffiles.length) - 1; random_page_selector = "random_html_code/random_" + numberoffiles[random_page_index] + ".html"; numberoffiles.splice(random_page_index, 1); // only removing one index, thus the 1 $("#random_load3").load ( random_page_selector, function() { $("#random_load3").trigger("create"); } ); The above code is for use with jquerymobile I'm unsure how I can optimise the above I'm unsure how to use any number for the following code: $("#random_loadX").load ( random_page_selector, function() { $("#random_loadX").trigger("create"); } ); (X being the select number) Thanks in advance! OM Quote Link to comment https://forums.phpfreaks.com/topic/262749-need-array-creation-and-popping-from-the-middle-advice/#findComment-1346717 Share on other sites More sharing options...
OM2 Posted May 19, 2012 Author Share Posted May 19, 2012 figured it out (i just thought it would be more complex!): for(i = 1; i < 4; i++) { random_page_index = Math.ceil(Math.random()*numberoffiles.length) - 1; random_page_selector = "random_html_code/random_" + numberoffiles[random_page_index] + ".html"; numberoffiles.splice(random_page_index, 1); // only removing one index, thus the 1 var divname = "#random_load" + i; $(divname).load ( random_page_selector, function() { $(divname).trigger("create"); } ); } thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/262749-need-array-creation-and-popping-from-the-middle-advice/#findComment-1346721 Share on other sites More sharing options...
OM2 Posted May 19, 2012 Author Share Posted May 19, 2012 actually, i see u have ur var names with a $ i assume this is ok i prefer to have my vars be with a $ - helps me identify at a glance what's the recommended practice? to use $ or not to use? thanks Quote Link to comment https://forums.phpfreaks.com/topic/262749-need-array-creation-and-popping-from-the-middle-advice/#findComment-1346723 Share on other sites More sharing options...
smoseley Posted May 19, 2012 Share Posted May 19, 2012 Haha, just realized this is in the JS forum.... my post was in PHP. Try this: // Add 40 items to your array totalNums = 40; // or whatever var arr = []; for (var i = 1; i <= totalNumbs; i++) arr[] = i; // Echo them out in random order var c = arr.length; while (c > 0) { i = Math.floor(Math.random()*c); console.log(arr[i]); arr.splice(i,1); c = arr.length; } Quote Link to comment https://forums.phpfreaks.com/topic/262749-need-array-creation-and-popping-from-the-middle-advice/#findComment-1346724 Share on other sites More sharing options...
OM2 Posted May 19, 2012 Author Share Posted May 19, 2012 thanks for that also... i found out in JS u can't do: arr[] = i; this throws an error - think that is coming from php as well Quote Link to comment https://forums.phpfreaks.com/topic/262749-need-array-creation-and-popping-from-the-middle-advice/#findComment-1346733 Share on other sites More sharing options...
smoseley Posted May 19, 2012 Share Posted May 19, 2012 arr.push(i) or arr[arr.length] = i Quote Link to comment https://forums.phpfreaks.com/topic/262749-need-array-creation-and-popping-from-the-middle-advice/#findComment-1346740 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.