NLCJ Posted June 20, 2011 Share Posted June 20, 2011 Hello, I'm trying to create a little function that randomly selects the winners, then it has to remove the winners from the array where it was in. I created it like this: $participants = array(); function winners($participants, $roundnumber, $amount) { $roundnumberwinners = $winners.$roundnumber; $roundnumberwinners = array_rand($participants, $amount); //print_r($roundnumberwinners); //echo "<br />"; $values = array(); foreach($roundnumberwinners as $key => $value) { $values[] = $roundnumberwinners[$participants[$value]]; } //echo $roundnumber." - ".count($participants)."<br />"; $participants = array_diff($participants, $values); echo $roundnumber." - ".count($participants)."<br />"; } It perfectly selects the amount of winners I want, but it doesn't get them out of the original array ($participants). I call it with: $winnerstest = winners($participants, 1, 10); If anyone could help... I've been stuck on this for a few days now. Thanks, Chris Quote Link to comment https://forums.phpfreaks.com/topic/239890-array_diff/ Share on other sites More sharing options...
AMcHarg Posted June 20, 2011 Share Posted June 20, 2011 Shuffle your array, select the first as the winner and then severe that one from the array. Edit: Or where you want more than one winner, select the first however many from the array and then remove them from the array. Shuffling the array randomizes their order in the array, so selecting the first few in the array after it has been shuffled will always pull out random results. Quote Link to comment https://forums.phpfreaks.com/topic/239890-array_diff/#findComment-1232254 Share on other sites More sharing options...
NLCJ Posted June 20, 2011 Author Share Posted June 20, 2011 How do I remove them from the array? That's kind of my problem. Quote Link to comment https://forums.phpfreaks.com/topic/239890-array_diff/#findComment-1232256 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 20, 2011 Share Posted June 20, 2011 How do I remove them from the array? That's kind of my problem. How many are you trying to take off, array_shift will remove the first item in the array, and shift everything else down. http://www.php.net/manual/en/function.array-shift.php Quote Link to comment https://forums.phpfreaks.com/topic/239890-array_diff/#findComment-1232257 Share on other sites More sharing options...
NLCJ Posted June 20, 2011 Author Share Posted June 20, 2011 I get 10 random items from the array, I also want to remove those exact 10. Every item in the array is unique, so it should be possible, right? Because every item in the array is unique, I thought I could easily differ it from the rest. Either my thoughs are wrong or my code. Quote Link to comment https://forums.phpfreaks.com/topic/239890-array_diff/#findComment-1232259 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 20, 2011 Share Posted June 20, 2011 Maybe array_slice or array_splice? http://www.php.net/manual/en/function.array-slice.php http://www.php.net/manual/en/function.array-splice.php Quote Link to comment https://forums.phpfreaks.com/topic/239890-array_diff/#findComment-1232265 Share on other sites More sharing options...
AMcHarg Posted June 20, 2011 Share Posted June 20, 2011 Try the below as an alternative to your method. It should produce a random winner and then remove that player from the array. The same principal will work if more winners are required. $players = array("Player1","Player2","Player3","Player4"); shuffle($players); $winner = $players[0]; $players = array_slice($players, 1); Quote Link to comment https://forums.phpfreaks.com/topic/239890-array_diff/#findComment-1232267 Share on other sites More sharing options...
NLCJ Posted June 20, 2011 Author Share Posted June 20, 2011 Okay, I'm trying to use slice, but cannot get it to work; function winners($array, $roundnumber, $amount) { echo $roundnumber." ".$amount." - "; $winners.$roundnumber = array(); shuffle($array); for($i = 0; $i < $amount; $i++) { $winners.$roundnumber[] = $array[$i]; } $array = array_slice($array, $amount); shuffle($array); echo count($array)."<br />"; } I know the second shuffle isn't necessary, it just gives me a better feeling. I also replaces $participants with $array, since the array with participants was called $participanst, this was confusing. What isn't working? Well, the array gets more items in it, 1 per time I call it... Quote Link to comment https://forums.phpfreaks.com/topic/239890-array_diff/#findComment-1232291 Share on other sites More sharing options...
AMcHarg Posted June 20, 2011 Share Posted June 20, 2011 $array = array_slice($array, 0, $amount, false); Simply having array_slice on it's own doesn't work in the same way as it does with shuffle. Edit: Hey you changed it when I was writing the above! Is it still not working? Quote Link to comment https://forums.phpfreaks.com/topic/239890-array_diff/#findComment-1232294 Share on other sites More sharing options...
NLCJ Posted June 20, 2011 Author Share Posted June 20, 2011 Yup, I edited it. function winners($array, $roundnumber, $amount) { echo $roundnumber." ".$amount." - "; $winners.$roundnumber = array(); $array = shuffle($array); for($i = 0; $i < $amount; $i++) { $winners.$roundnumber[] = $array[$i]; } $array = array_slice($array, $amount); $array = shuffle($array); echo count($array)."<br />"; //print_r($winners.$roundnumber); } Nope, still not working... I get the following errors: Warning: array_slice() expects parameter 1 to be array, boolean given in ... on line 34 Warning: shuffle() expects parameter 1 to be array, null given in ... on line 35 Cannot figure out what's wrong... Quote Link to comment https://forums.phpfreaks.com/topic/239890-array_diff/#findComment-1232297 Share on other sites More sharing options...
AMcHarg Posted June 20, 2011 Share Posted June 20, 2011 $winners.$roundnumber[] = $array[$i]; Does this line actually work? It's meant to be a variable variable... right? I thought you required two $ at the beginning... $$winners.$roundnumber[] = $array[$i]; Might not be connected to the errors you are receiving though. Quote Link to comment https://forums.phpfreaks.com/topic/239890-array_diff/#findComment-1232308 Share on other sites More sharing options...
AbraCadaver Posted June 20, 2011 Share Posted June 20, 2011 Try this (make sure to use the &). I don't see why you need the $roundnumber: function winners(&$participants, $amount) { shuffle($participants); return array_splice($participants, 0, $amount); } $winnerstest = winners($participants, 10); Quote Link to comment https://forums.phpfreaks.com/topic/239890-array_diff/#findComment-1232311 Share on other sites More sharing options...
NLCJ Posted June 20, 2011 Author Share Posted June 20, 2011 Thank you, the & did the trick! And thanks to all others who helped me, too! Quote Link to comment https://forums.phpfreaks.com/topic/239890-array_diff/#findComment-1232317 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.