Jump to content

[SOLVED] Generate Combinations


Minase

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/108066-solved-generate-combinations/
Share on other sites

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
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.