Jessica Posted July 29, 2012 Share Posted July 29, 2012 I need some logical help on this one. I have an object which has X settings, which are on/off. It also can have Y settings which each have Z number of options. Given the numbers, I want to figure out how many possible combinations of settings there are. For example, X=2, Y=2, y[0] = 3, y[1] = 4 So there are two on/off settings, 1 setting with 3 options, and 1 setting with 4 options. How would I compute the different settings? I have tried several times to create a recursive function for it, but I can't get it to return the output in a way that makes sense. I think I need something similar to this: www.php.net/manual/en/function.shuffle.php#90615 But going above 0 or 1. is this even possible? Link to comment https://forums.phpfreaks.com/topic/266415-permutations-of-setscombinations/ Share on other sites More sharing options...
Barand Posted July 29, 2012 Share Posted July 29, 2012 There would be 2*2*3*4 = 48 combinations. The function will take a little longer Link to comment https://forums.phpfreaks.com/topic/266415-permutations-of-setscombinations/#findComment-1365271 Share on other sites More sharing options...
Christian F. Posted July 29, 2012 Share Posted July 29, 2012 Hmm... Wouldn't something like this work? function FindPerms ($Settings) { $Perms = 0; foreach ($Settings as $Options => $Number) { $Perms += pow ($Number, $Options); } return $Perms; } Link to comment https://forums.phpfreaks.com/topic/266415-permutations-of-setscombinations/#findComment-1365277 Share on other sites More sharing options...
Barand Posted July 29, 2012 Share Posted July 29, 2012 as promised <?php $sets = array( array (0,1), array (0,1), array ('x','y','z'), array ('a','b','c','d') ); $results = array(); // find all combinations combies($sets, $results, array()); // show results foreach($results as $a) { echo join('-', $a) . '<br />'; } function combies($sets,&$results, $arr) { $ks = count($sets); $tmp = array_shift($sets); foreach($tmp as $x) { if ($ks==1) { $results[] = array_merge($arr, array($x)); } else { combies($sets, $results, array_merge($arr, array($x))); } } } ?> Link to comment https://forums.phpfreaks.com/topic/266415-permutations-of-setscombinations/#findComment-1365289 Share on other sites More sharing options...
Jessica Posted July 30, 2012 Author Share Posted July 30, 2012 Barand, you're amazing. The code I'm working on is at home and I'm at work, so I will have to test it out with my code tonight or tomorrow, but it looks exactly like what I was trying to do. Link to comment https://forums.phpfreaks.com/topic/266415-permutations-of-setscombinations/#findComment-1365420 Share on other sites More sharing options...
Jessica Posted August 1, 2012 Author Share Posted August 1, 2012 Finally got a chance to come back to this project, Barand the code works perfectly. Thanks again. Link to comment https://forums.phpfreaks.com/topic/266415-permutations-of-setscombinations/#findComment-1365850 Share on other sites More sharing options...
Barand Posted August 1, 2012 Share Posted August 1, 2012 Glad to help. I enjoy a challenge to the old grey cells. Link to comment https://forums.phpfreaks.com/topic/266415-permutations-of-setscombinations/#findComment-1365853 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.