Skip to content

Instantly share code, notes, and snippets.

@kopepasah
Created December 1, 2013 19:04
Show Gist options
  • Save kopepasah/7739391 to your computer and use it in GitHub Desktop.
Save kopepasah/7739391 to your computer and use it in GitHub Desktop.
A simple function for mimicking drawing names from a hat.
<?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