note 71290 deleted from control-structures.foreach by nlopess

From: Date: Thu, 25 Jan 2007 14:24:00 +0000
Subject: note 71290 deleted from control-structures.foreach by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-122188@lists.php.net to get a copy of this message
Note Submitter: Elwin van Huissteden ---- Does everyone know the trick when you put names of people on small pieces of paper, put them in a hat, and let those people pull out the papers to see who they have to give a gift to? I made that into a function for a site where people can do this. here is the essential part of that function (i changed any important stuff from that site into '...'): $query = '...'; if ($groepen = mysql_query($query)); else die(mysql_error()); $A1 = array(); $A2 = array(); $A3 = array(); // I couldnt find a good function to copy an array, so i used this: while ($grp = mysql_fetch_array($groepen)) { array_push($A1,$grp['...']); array_push($A2,$grp['...']); } foreach($A1 as $S1 => $S2) { if (count($A2) >= 2) { shuffle($A2); shuffle($A2); // I shuffled it twice because computers arent good at random functions while (end($A2) == $S2) shuffle($A2); array_push($A3,array($S2,array_pop($A2))); } else if (count($A2) == 1) { if (end($A2) == $S2) { array_push($A3,array($S2,$A3[0][1])); $A3[0][1] = array_pop($A2); } else array_push($A3,array($S2,array_pop($A2))); } } This was copied from the source of: http://www.mijnwenslijst.nl/ you could check out the function yourself, but the site is completely in Dutch...

« previous php.notes (#122188) next »