PHP to Assign Secret Santa

December 12, 2009

in Programming

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.

  1. <?php
  2. echo "Christmas Secret Santa<br>\n";
  3.  
  4. //Build the two arrays. I could in theory use a single array and double it but I was lazy and just defined it twice.
  5. $giver = array('Rudolph','Donner','Blitzen','Santa');
  6. $receiver = array('Rudolph','Donner','Blitzen','Santa');
  7.  
  8. //Randomize the two arrays
  9. shuffle($giver);
  10. shuffle($receiver);
  11.  
  12. //We're going to iterate through the giver array for the individuals.
  13. foreach ($giver as $g) {
  14.  //Make sure that the giver and the receiver are not the same person
  15.  while ($receiver[0] == $g) {
  16.   //Shuffle the array to randomize it.
  17.   shuffle($receiver);
  18.  }
  19.  //Grab the first person off the receiver array
  20.  $r = $receiver[0];
  21.  //Echo out the result
  22.  echo $g." – ".$r."<br>\n";
  23.  //Remove that first person from the array, so we only have ungifted people remaining.
  24.  $receiver = array_slice($receiver,1);
  25. }?>

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

Leave a Comment

Previous post:

Next post: