Re: List all possible 2 letter combinations of 8 letters

From: Date: Mon, 20 Nov 2000 20:16:10 +0000
Subject: Re: List all possible 2 letter combinations of 8 letters
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-26326@lists.php.net to get a copy of this message
Based of Jasons post, here is a little function to do your bidding : <?php // letters to analyze $letters = array ('a','b','c','d','e','f','g','h'); // To execute : MakeCombos($letters); // Provides : $combos_count = number of combos // $combos = array of all combos function MakeCombos($array) { global $combos,$combos_count; $max = count($array); for ($n0 = 0; $n0 < $max; $n0++) { for ($n1 = 0; $n1 < $max; $n1++) { $combos[] = $array[$n0] . $array[$n1]; } } $combos_count = count($combos); } ?> Had all these master plans to expand this but then said to self, "why? stop straining your brain" so i stopped :-) If others would like to add configurations such as "combo size","repeat on/off" ... feel free! Philip -- be outside the box. On Mon, 20 Nov 2000, Jason Murray wrote: > Ben Weinberger wrote: > > > > Hi~ > > I'm trying to write a script that will list all 2 letter combinations of > > given number of letters. For example, let's say I entered 8 letters "a, b, > > c, d, e, f, g, h". I want the script to spit out all 2 letter > > combinations-- i.e. ab, ac, ad, ae, etc.... Also, the script should make > > sure it doesn't repeat any combinations-- I really have no idea where to > > start on this one-- the only thing I know is that it will most likely be > > within some sort of for/while loop... can someone help? > > > > Thanks, > > -Ben > > > > Off the top of my head... > > > $foo = { "a", "b", "c", "d", "e", > "f", "g", "h" }; > $dim = 8; > > for ($n0 = 0; n0 < dim; n0++) { > for (n1 = 0; n1 < dim; n1++) { > echo $foo[n0] . $foo[n1]); > } > } > > > Greetings, > Jason > -- > Jason Murray > Developer > jWeb New Media Design > 1 877 525 jWEB > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#26326) next »