Jump to content

Random Ordered List


bschultz

Recommended Posts

I have a text file with this content:

 

item1

item2

item3

item4

item1

item3

item1

 

So, item1 is in the list 3 times, item2 just once and item3 is in twice...as is item4.  The list is 36 items long.  I need to create a random order list for these items.  Ideally, I'd like to have this format:

 

item1|3

item2|1

item3|2

item4|2

 

 

which is item|times appearing in the list

 

I know that this code

$filename="words.txt";
$words=file($filename);
shuffle($words);
$word=$words[0];
echo $word;

will give me a random list, but not for the repeated stuff...and I'd like to limit it so that item1 can't repeat in proximity to another item1 in the list.  I have no idea how to pull this off.  Any ideas?

 

Thanks.

 

Brian

Link to comment
Share on other sites

Thanks for the help, but it's not quite what I was looking for.  I wasn't real clear in my original post.

 

In my text file, I want item|# of times it needs to appear in the array.

 

So, snow|2  in the text file needs to put the word snow in the list twice

 

and, rain|4 in the text file needs to put rain in the array 4 times.

 

Thanks again!

Link to comment
Share on other sites

Oh. Well then use the code you already posted:

 

$filename="words.txt";
$words=file($filename);
shuffle($words);
$word=$words[0];
echo $word;

 

file will take all the lines in words.txt and put them into an array, so if there's 4 lines with item1 in your file, it will be in the array 4 times.  Then you just shuffle it and pick one.  Now, you did say this:

 

and I'd like to limit it so that item1 can't repeat in proximity to another item1 in the list.

 

That will be hard, if not altogether impossible to accomplish.  What if you have like 10 item1's and 2 item2's and 1 item3's?  I have no idea what your data range/occurrence is/will be but that's something you also need to explain or at least consider.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.