Skip to content

Instantly share code, notes, and snippets.

@bogomil
Last active February 16, 2017 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bogomil/f9d2e3ffbb3032de1f69 to your computer and use it in GitHub Desktop.
Save bogomil/f9d2e3ffbb3032de1f69 to your computer and use it in GitHub Desktop.
<?php
/*
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2014 Bogomil Shopov <shopov.bogomil@gmail.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
*/
//Keep the result here
$res = array( );
//Add all participants in this array. Add as many as you need.
$arr = array( 'John','Jane','George','Adolf','Ronnie','Igor','Peter' );
// retrieve the first element and save it
$first = $ele1 = array_shift( $arr );
while( count( $arr ) ) {
// get a random element
$ele2 = array_rand( $arr );
// associate elements
$res[ $ele1 ] = $arr[ $ele2 ];
// random element becomes next element
$ele1 = $arr[ $ele2 ];
// delete the random element
array_splice( $arr, $ele2, 1 );
}
// associate last element with the first one
$res[ $ele1 ] = $first;
//display the assignments. Have Fun!
foreach ( $res as $key => $value ) {
echo $res[ $key ] ." gives a present to ". $res[ $value ]."<br/>";
}
/*Alternatively you can add e-mail address to the names and a simple mail()
function to send the result to every participant.
Read more about Secret Santa here: http://en.wikipedia.org/wiki/Secret_Santa
Happy Holidays!
*/
?>
@yurukov
Copy link

yurukov commented Dec 4, 2014

Put Abraham and Ahmed in the example names as well. Then we could get "Adolf gives a present to Abraham".

@bogomil
Copy link
Author

bogomil commented Feb 16, 2017

+1:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment