junelis Posted October 5, 2008 Share Posted October 5, 2008 Hi friends can someone tall me how to make code who will calculate all combinations from one array. example : array(2, 5, 7, 3, 4, 6) examle: 2, 5, 7 2, 5, 6 2, 5, 3 2, 5, 7, 3 ... if I i want 3/6 should be 20 combinations if I want 4/6 should be 10 combinations or 5/6 6 combin. can someone give idea how to make this Thanks !!! Quote Link to comment https://forums.phpfreaks.com/topic/127115-combinations/ Share on other sites More sharing options...
genericnumber1 Posted October 5, 2008 Share Posted October 5, 2008 I don't quite understand what you want. Count the number of permutations? it's just n factorial (n! where n is the number of elements) Find all possible permutations? Find a certain number of permutations based upon the number of elements? Based upon certain elements? That's a bit more complicated, and no point giving you code for one question when you may have another. Quote Link to comment https://forums.phpfreaks.com/topic/127115-combinations/#findComment-657713 Share on other sites More sharing options...
Acs Posted October 5, 2008 Share Posted October 5, 2008 I think if just looks for the word "permutations" he will already have plenty of code Sometimes I think that knowing what you want or what to look for is the hardest part in our software world! Quote Link to comment https://forums.phpfreaks.com/topic/127115-combinations/#findComment-657760 Share on other sites More sharing options...
genericnumber1 Posted October 5, 2008 Share Posted October 5, 2008 I think if just looks for the word "permutations" he will already have plenty of code Sometimes I think that knowing what you want or what to look for is the hardest part in our software world! for sure Quote Link to comment https://forums.phpfreaks.com/topic/127115-combinations/#findComment-657780 Share on other sites More sharing options...
junelis Posted October 6, 2008 Author Share Posted October 6, 2008 Ok i will give one more examlple: array(green, fly, blue, dry, small); from this 5 word I want all combinatitons from by 3 word we have 3 from 5 that is 10 combinations 1. green, fly, blue 2. green, fly, dry 3. green, fly, small 4. green, blue, dry 5. green, dry, small 6. green, blue, small 7. fly, blue, dry 8. fly, blue, small 9. blue, dry, small 10. fly, dry, small but i can`t make this in code. please if someone know to post it. Quote Link to comment https://forums.phpfreaks.com/topic/127115-combinations/#findComment-658049 Share on other sites More sharing options...
waynew Posted October 6, 2008 Share Posted October 6, 2008 http://php.happycodings.com/Algorithms/code21.html http://www.phpclasses.org/browse/package/200.html http://www.webmasterworld.com/forum88/778.htm Quote Link to comment https://forums.phpfreaks.com/topic/127115-combinations/#findComment-658050 Share on other sites More sharing options...
sasa Posted October 6, 2008 Share Posted October 6, 2008 try <?php function my_permut($a, $num, $start = 0){ if(!is_array($a)) return false; if (count($a) == $num) return array($a); if ($num == 1){ $out = array(); for ($i = $start; $i < count($a); $i++) $out[]=array($a[$i]); return $out; } $a = array_values($a); $out = array(); for ($i = $start; $i <= count($a) - $num; $i++){ $d = my_permut($a, $num-1, $i+1); foreach ($d as $e){ array_unshift($e,$a[$i]); $out[] = $e; } } return $out; } $x = my_permut(array(1,2,3,4,5),3); print_r($x); ?> Quote Link to comment https://forums.phpfreaks.com/topic/127115-combinations/#findComment-658125 Share on other sites More sharing options...
junelis Posted October 7, 2008 Author Share Posted October 7, 2008 OK i will try this code but i dont think that will be ok because I want combinations not premutations in combinations 1. green, fly, blue 2. fly, green, blue are same but not in permitation once again please help me if you have code for combinations. Thanks !!! Quote Link to comment https://forums.phpfreaks.com/topic/127115-combinations/#findComment-659277 Share on other sites More sharing options...
sasa Posted October 7, 2008 Share Posted October 7, 2008 i changed my permutation function but i don't change its name sorry Quote Link to comment https://forums.phpfreaks.com/topic/127115-combinations/#findComment-659321 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.