Page 1 of 1

Combinations with multiple arrays

Posted: Wed Oct 12, 2005 5:59 pm
by EsCh3r
Hi, am working on a system that requires getting all posible combinations between multiple arrays, the number of arrays is variable as is the number of items inside the array. EJ.

Code: Select all

$categories = array();
array_push($categories, array(1,2,3,4,5,6,7));
array_push($categories, array("white", "blue", "red", "green", "purple"));
array_push($categories, array("one", "two", "three", "four"));
I tried doing a function for this but failed quite badly, I just can't seem to get my head around this, so if anyone can point me in the right direction it'd be great. Here's the function I was doing but it doesn't work as it should, it only gives some of the combinations.

Code: Select all

function allPosibleCombinations($categories)
{
    $counter = array();
    for($j=0; $j<count($categories); $j++)
    {
        $counter[$j] = 0;
    }
    $paf = 0;
    for($i=0; $i<count($categories); $i++)
    {
        echo "#i=".$i."<br>";
        for($j=$i; $j<count($categories); $j++)
        {
            echo "#j=".$j."<br>";
            for($k=$i; $k<count($categories[$j]); $k++)
            {
                $paf ++;
                $echoStr = $paf.">>";
                for($l=0; $l<count($categories); $l++)
                {
                    $echoStr .= $categories[$l][$counter[$l]];
                }
                echo $echoStr."<br>";
                $counter[$j]++;
            }
            $counter[$j] = 0;
        }
    }
}
Thanks a lot.

Posted: Wed Oct 12, 2005 6:32 pm
by EsCh3r
Just redid it and it's much improved

Code: Select all

function allPosibleCombinations($arrays)
{
    $counter = array();
    for($j=0; $j<count($arrays); $j++)
    {
        $counter[$j] = 0;
    }
    $paf = 0;
    $done = false;
    while (!$done && $paf < 2000)
    {
        $paf ++;
        $echoStr = $paf.">>";
        for($i=0; $i<count($arrays); $i++)
        {
            $echoStr .= /*"(".$counter[$i].")".*/$arrays[$i][$counter[$i]]."-";
        }
        echo $echoStr."<br>";
        for($i=0; $i<count($arrays); $i++)
        {
            $counter[$i]++;
            if($counter[count($arrays)-1] == count($arrays[count($arrays)-1]))
            {
                $done = true;
                break 2;
            }
            if($counter[$i] >= count($arrays[$i]))
            {
                // echo "reseteando";
                $counter[$i]=0;
            }
            else
            {
                break;
            }
        }
    }
}
Only thing now is I don't know how to validate it's correct, how can I know if it's really creating all combinations and not reapeating any combinations?

Thanks

Posted: Thu Oct 13, 2005 9:48 am
by pickle
Manually plug through it. Give it some test arrays and then manually permute all possible combinations (a couple arrays of length 3 should work).

Posted: Thu Oct 13, 2005 9:58 am
by foobar
If you have a calculator such as the Ti83/84 and better, you can calculate the permutations on it and check the results with your php function.

Posted: Thu Oct 13, 2005 10:57 am
by EsCh3r
Actually I messed up in the post title it wasn't permutations what I wanted but combinations... my bad

Posted: Thu Oct 13, 2005 11:24 am
by pickle
Either way, you're gonna have to do some manual work to double check. My earlier post should still apply.