Combinations with multiple arrays
Posted: Wed Oct 12, 2005 5:59 pm
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.
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.
Thanks a lot.
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"));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;
}
}
}