Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/239890-array_diff/
Share on other sites

Shuffle your array, select the first as the winner and then severe that one from the array.  8)

 

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.

Link to comment
https://forums.phpfreaks.com/topic/239890-array_diff/#findComment-1232254
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/239890-array_diff/#findComment-1232259
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/239890-array_diff/#findComment-1232267
Share on other sites

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. :P

 

What isn't working? Well, the array gets more items in it, 1 per time I call it...  :shrug:

Link to comment
https://forums.phpfreaks.com/topic/239890-array_diff/#findComment-1232291
Share on other sites

Yup, I edited it. :P

 

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... :(

 

Link to comment
https://forums.phpfreaks.com/topic/239890-array_diff/#findComment-1232297
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/239890-array_diff/#findComment-1232311
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.