Snooble Posted August 9, 2009 Share Posted August 9, 2009 Hello! Thanks in advance.. here's my problem: I want the code to fill the array with 10 unique values from my list. Here's my code now: $bands = "Bloc Party, Arcade Fire, DFA 1979, Klaxons, Metric, Blur, Small Faces, Smiths, Stripes, Charlatans, MGMT, Strokes"; $bands = explode(", ",$bands); $amountofbands = count($bands); $no[] = $bands[rand(0, $amountofbands)]; $no[] = $bands[rand(0, $amountofbands)]; $no[] = $bands[rand(0, $amountofbands)]; $no[] = $bands[rand(0, $amountofbands)]; $no[] = $bands[rand(0, $amountofbands)]; $no[] = $bands[rand(0, $amountofbands)]; $no[] = $bands[rand(0, $amountofbands)]; $no[] = $bands[rand(0, $amountofbands)]; $no[] = $bands[rand(0, $amountofbands)]; $no[] = $bands[rand(0, $amountofbands)]; array_unique($no); while(count($no) != 10){ $no[] = $bands[rand(0, $amountofbands)]; $no = array_unique($no); } print_r($no); This gives me results that sometimes are blank, and there are also duplicates. How can i fix it!??!?! :facewall: Thank you! Snooble Quote Link to comment https://forums.phpfreaks.com/topic/169480-solved-unique-random-array-please-help/ Share on other sites More sharing options...
wildteen88 Posted August 9, 2009 Share Posted August 9, 2009 This $no = array_unique($no); Should be just array_unique($no); However aren't you better of with just using shuffle as all you're doing is randomising the order of the array. Quote Link to comment https://forums.phpfreaks.com/topic/169480-solved-unique-random-array-please-help/#findComment-894186 Share on other sites More sharing options...
Snooble Posted August 9, 2009 Author Share Posted August 9, 2009 My aim: There are 20 items in a list. I want it to pick a random 10 of those 20. None can be the same. What do you think is the easiest way? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/169480-solved-unique-random-array-please-help/#findComment-894190 Share on other sites More sharing options...
wildteen88 Posted August 9, 2009 Share Posted August 9, 2009 You can use array_rand. There are many functions for array's check out the many on array's Quote Link to comment https://forums.phpfreaks.com/topic/169480-solved-unique-random-array-please-help/#findComment-894192 Share on other sites More sharing options...
PFMaBiSmAd Posted August 9, 2009 Share Posted August 9, 2009 Your blank entries are because count($bands) is one more than the highest array index so randomly you will get a NULL value for a nonexistent array entry. array_unique($no); does nothing because it returns an array that you must do something with. No comment on your while() loop. After you explode() the string, just use shuffle() on the array and then use the first 10 entries. Quote Link to comment https://forums.phpfreaks.com/topic/169480-solved-unique-random-array-please-help/#findComment-894196 Share on other sites More sharing options...
Snooble Posted August 9, 2009 Author Share Posted August 9, 2009 Thanks PFMaBiSmAd Definitely best idea is to shuffle and just use the first ten. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/169480-solved-unique-random-array-please-help/#findComment-894205 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.