Jump to content

Permutations of Sets/Combinations?


Jessica

Recommended Posts

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

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)));
        }
    }
}    

?>

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.