Last active
February 16, 2017 11:54
-
-
Save bogomil/f9d2e3ffbb3032de1f69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2014 Bogomil Shopov <[email protected]> | |
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! | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Put Abraham and Ahmed in the example names as well. Then we could get "Adolf gives a present to Abraham".