note 71290 added to control-structures.foreach

From: Date: Tue, 21 Nov 2006 11:15:01 +0000
Subject: note 71290 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-119935@lists.php.net to get a copy of this message
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... ---- Server IP: 83.137.20.120 Probable Submitter: 82.92.27.234 ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit -- https://master.php.net/note/edit/71290 Del: integrated -- https://master.php.net/note/delete/71290/integrated Del: useless -- https://master.php.net/note/delete/71290/useless Del: bad code -- https://master.php.net/note/delete/71290/bad+code Del: spam -- https://master.php.net/note/delete/71290/spam Del: non-english -- https://master.php.net/note/delete/71290/non-english Del: in docs -- https://master.php.net/note/delete/71290/in+docs Del: other reasons-- https://master.php.net/note/delete/71290 Reject -- https://master.php.net/note/reject/71290 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119935) next »