Combinations with multiple arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
EsCh3r
Forum Newbie
Posts: 3
Joined: Wed Oct 12, 2005 5:47 pm

Combinations with multiple arrays

Post 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.
Last edited by EsCh3r on Thu Oct 13, 2005 10:58 am, edited 1 time in total.
EsCh3r
Forum Newbie
Posts: 3
Joined: Wed Oct 12, 2005 5:47 pm

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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.
EsCh3r
Forum Newbie
Posts: 3
Joined: Wed Oct 12, 2005 5:47 pm

Post by EsCh3r »

Actually I messed up in the post title it wasn't permutations what I wanted but combinations... my bad
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Either way, you're gonna have to do some manual work to double check. My earlier post should still apply.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply