bschultz Posted November 17, 2008 Share Posted November 17, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/133118-random-ordered-list/ Share on other sites More sharing options...
.josh Posted November 17, 2008 Share Posted November 17, 2008 $list = file("words.txt"); sort($list); $counts = array_count_values($list); $list = array_unique($list); $list = array_values($list); foreach($list as $key => $val) { $list[$key] = "$val|{$counts[$val]}"; } echo "<pre>"; print_r($list); echo "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/133118-random-ordered-list/#findComment-692287 Share on other sites More sharing options...
bschultz Posted November 17, 2008 Author Share Posted November 17, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/133118-random-ordered-list/#findComment-692297 Share on other sites More sharing options...
.josh Posted November 17, 2008 Share Posted November 17, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/133118-random-ordered-list/#findComment-692360 Share on other sites More sharing options...
bschultz Posted November 17, 2008 Author Share Posted November 17, 2008 I thought that might be kinda hard (if not impossible) to do...thanks anyway! Quote Link to comment https://forums.phpfreaks.com/topic/133118-random-ordered-list/#findComment-692373 Share on other sites More sharing options...
Barand Posted November 18, 2008 Share Posted November 18, 2008 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. If you apply a limit like that then it's no longer random - you can't have both Quote Link to comment https://forums.phpfreaks.com/topic/133118-random-ordered-list/#findComment-692428 Share on other sites More sharing options...
cooldude832 Posted November 18, 2008 Share Posted November 18, 2008 you can try and yes not random <?php $filename="words.txt"; $words=file($filename); $i = 0; $list = 4; while($i <$list){ shuffle($words); $word=$words[0]; if($word != $last_word){ echo $word; $last_word = $word; $i++; } } ?> Just don't use it with a big list and big array Quote Link to comment https://forums.phpfreaks.com/topic/133118-random-ordered-list/#findComment-692432 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.