Secret Santa Name Picker Script

In the last ten years we’ve been gathering for our annual Pelago holiday party we’ve developed a fun Secret Santa tradition. The theme of the gift exchange is t-shirts. Each person picks a name from a hat to find out the lucky (or unlucky, depending on how you look at it) recipient of a hand-selected t-shirt. With one employee going remote and us being a technology company, it seemed somewhat Ludditean of us to continue picking strips of paper from a box.

This year we automated the process. We wrote a PHP script that would pick the names for us. It starts with an array of email addresses, matches them up in a random order and then emails each person their recipient. And it makes sure no one gets the same person. Take a look at the script below. It’s just a few simple lines of code. Feel free to use it for your next Secret Santa assignments. The basic code is below. Just plug the results in to your favorite email software to secretly distribute the results.


<?php

$emails = array(
     'name1@yourcompany.com',
     'name2@yourcompany.com',
     'name3@yourcompany.com',
     'name4@yourcompany.com',
     'name5@yourcompany.com',
);
$matches = array();

// randomize a key => value array
$givers = range(0,count($emails)-1);
$givees = range(0,count($emails)-1);

do {

     shuffle($givers);
     shuffle($givees);

     $matches = array_combine($givers, $givees);

     foreach ($matches as $er => $ee) {
          if ($er == $ee) continue 2;
     }

     break;

} while (true);

foreach ($matches as $er => $ee) {
     echo $emails[$er] . " gives to " . $emails[$ee] . "\n";
}

?>

Tags: , , ,
Bookmark: Post to Del.icio.us Post to Digg Post to Google Post to Ma.gnolia Post to MyWeb Post to Newsvine Post to Reddit Post to Simpy Post to Slashdot Post to Technorati

2 Responses to “Secret Santa Name Picker Script”

  1. newest submissions : programming Says:

    Kramer auto Pingback[...] like or dislike and help decide what's popular, or submit your own! new|rising1•••Secret Santa PHP Script (pelagodesign.com)submitted 22 [...]

  2. Secret Santa PHP Script Says:

    Kramer auto Pingback[...] johnjreeve via pelagodesign.com Submitted: Nov 22 2010 / 16:06 Secret Santa PHP Script A simple PHP script to pick secret santa recipients Vote up Vote [...]

Leave a Reply