PHP to Assign Secret Santa

Because I’m nerdy I decided to write a quick PHP script to handle the assigning of people for my family’s Secret Santa. It’s short, simple, and not-error checked or bug proof in the least.


//Build the two arrays. I could in theory use a single array and double it but I was lazy and just defined it twice.
$giver = array('Rudolph','Donner','Blitzen','Santa');
$receiver = array('Rudolph','Donner','Blitzen','Santa');

//Randomize the two arrays

//We're going to iterate through the giver array for the individuals.
foreach ($giver as $g) {
	//Make sure that the giver and the receiver are not the same person
	while ($receiver[0] == $g) {
		//Shuffle the array to randomize it.
	//Grab the first person off the receiver array
	$r = $receiver[0];
	//Echo out the result
	echo $g." - ".$r."
\n"; //Remove that first person from the array, so we only have ungifted people remaining. $receiver = array_slice($receiver,1); }?>

So nerdy. Maybe later I’ll do it as a javascript snippet.

2 thoughts on “PHP to Assign Secret Santa”

  1. Hello, first of all, thanks a lot, this really help me.

    I’ve just did a change.

    //Randomize the two arrays
    $ngiver = count($giver)-1;
    $nreceiver = count($receiver)-1;
    }while($giver[$ngiver] == $receiver[$nreceiver]);

    every time when I was trying the code, if the last value in array is the same as the other array it brokes. I’ve add a condition to verify this case and just let it flow if they aren’t the same.

    so, I hope to help someone as this post help me. Thanks again.

    PS: Sorry for my english. I’m a Brasilian LOL

  2. **Exclude participant drawing another participant’s name?**
    How would we exclude certain participants from being randomly selected another participant’s name? Use case example: Family member A doesn’t talk to family member Z.

Comments are closed.