Minase Posted May 30, 2008 Share Posted May 30, 2008 hello,i did try to create a script that autogenerate an list with avaible combinations,but i did not succed,here is what i need to do: we have a character range from 1-9 and from a till b i want it to print whole combinations possible separated by underline "_" eg: a b a_1 .. a_9 b_1 .. b_9 etc etc.. the maximum character combination to be set in a variable $a = 5; from a till b_9_9_9_9; that will be the return Quote Link to comment https://forums.phpfreaks.com/topic/108066-solved-generate-combinations/ Share on other sites More sharing options...
.josh Posted May 31, 2008 Share Posted May 31, 2008 well here's my take. I'm sure there's a way more elegant way of doing it, but whatever. <?php $alpha = array ('a','b'); // alpha prefixes $size = 3; // dummy var for number of combos including alpha prefix // for each prefix in the array foreach ($alpha as $a) { // you want a combo of $size digits so here goes... for ($x = 1; $x < $size; $x++) { $max.="9"; // since 9999..is largest number in any set.. } // end for // let's start counting... for ($x = 1; $x <= $max; $x++) { // we need to split up the digits so we can... $w = preg_split('//',$x); // so we can put a _ between them. Also trim the last one off $w = $a . rtrim(implode('_',$w),'_'); // echo current iteration (or save to an array or whatever here) echo "$w <br />"; } // end for // reset $max for next alpha prefix $max = 0; } // end foreach ?> Quote Link to comment https://forums.phpfreaks.com/topic/108066-solved-generate-combinations/#findComment-553958 Share on other sites More sharing options...
Minase Posted May 31, 2008 Author Share Posted May 31, 2008 thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/108066-solved-generate-combinations/#findComment-554049 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.