spfoonnewb Posted April 9, 2007 Share Posted April 9, 2007 Hi, I have created this script to generate number combinations for variables given, with the amount of variables selected.. So say you choose the number 10, and 3 total variables, the following should appear: 5 + 2 + 3, 2 + 4 + 6 ..and so on I have written the script below, and it only displays combinations where any extra numbers other than the first one are the same.. I need the numbers to all be different. So instead of what I want, it shows like: 4 + 2 + 2 8 + 1 + 1 ..and so on The second two (or more) numbers are always the same.. <?php //Number you wish to add up to $n = 10; //How many total variables there is already 2 present.. so its + how ever many $w = 1; for ($ima = 1; $ima <= $n; ++$ima) { $num[] = $ima; foreach ($num as $v) { $nar = array(''); $str = "$ima + $v ".str_repeat("+ $v ", $w).""; if (!in_array($str, $nar)) { $nar[] = $str; } echo "<pre>".print_r($nar)."</pre>"; } } ?> Quote Link to comment 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.