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

?>

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.