Created
December 1, 2013 19:04
-
-
Save kopepasah/7739391 to your computer and use it in GitHub Desktop.
A simple function for mimicking drawing names from a hat.
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 | |
function draw_names_from_hat( $names = array(), $couples = false ) { | |
$hat = $names; | |
foreach ( $names as $key => $name ) { | |
$pick = array_rand( $hat ); | |
if ( $name == $hat[$pick] ) { | |
unset( $hat[$pick] ); | |
$pick = array_rand( $hat ); | |
array_push( $hat, $name ); | |
} | |
if ( $couples == true ) { | |
if ( $key % 2 == 0 && $key + 1 == $pick ) { | |
$paper = $hat[$pick]; | |
unset( $hat[$pick] ); | |
$pick = array_rand( $hat ); | |
array_push( $hat, $paper ); | |
} | |
if ( $key % 2 == 1 && $key - 1 == $pick ) { | |
$paper = $hat[$pick]; | |
unset( $hat[$pick] ); | |
$pick = array_rand( $hat ); | |
array_push( $hat, $paper ); | |
} | |
} | |
$paired[$name] = $hat[$pick]; | |
unset( $hat[$pick] ); | |
} | |
foreach ( $paired as $name => $pick ) { | |
echo $name . ' = ' . $pick . '<br/>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment