I've seen lots of people asking for how to generate all the possible combinations of multiple arrays and all I've seen in the responses is either how to specifically solve that one problem, i.e. to X amount of levels or how to do it using a single string as the input and generating all the combinations of single letters.
I too needed a way to take 3,4,5... 100 arrays full of values and generate all the possible combinations. After many long hours of slamming my head against the wall trying to get some type of recursive nested function system to work, I ended up coming up with a totally different approach.
I present this approach to the masses for criticism and to help out those who haven't found this solution anywhere else. Please let me know what you think.
The solution is only 31 lines of PHP code, but I've added lots of comments so it's easy to understand what I was trying to do to those who aren't so savvy at reading other's code.
Thanks a ton to everyone who helps out on here!
PLEASE NOTE: This solution does NOT create all combinations relating to placement of values.. i.e. it will NOT generate abc, acb, bca, bac, cab, cba.
<?php
// This function builds out all the possible combinations of values
// supplied to it by an infinite amount of arrays. I searched PHPBuilder
// for a similar function and no one had made one that wasn't limited or
// could accept non-single-character values.
function buildPermutationResult($arraySet) {
// Count the number of data sets we're passing the function
$numSets = count($arraySet);
// Let's loop through our data sets so we can generate some statistical
// info that will be helpful later.
for($i=0;$i<$numSets;$i++) {
// Get the number of variables in each set
$numVars = count($arraySet[$i]);
// Do a cumulative multiplication of the number of variables in each set
// If we're on the first set, set the number of vars as our total as
// we don't want to try to multiple the first number by 0.
if($i == 0) $totalCombos = $numVars;
else $totalCombos = $totalCombos * $numVars;
// Store our stat info for user later.
$statInfo[$i]['numVars'] = $numVars;
$statInfo[$i]['totalCombos'] = $totalCombos;
}
// Setup our variables that keep track of where we are in the permutation
$key = 0;
$rotationCounter = 1;
// Loop through our sets again, but this time we're going to build our
// our combinations using the statistical data we generated earlier.
for($setNum=0;$setNum<$numSets;$setNum++) {
// Get the statistical data we generated earlier
$numVars = $statInfo[$setNum]['numVars'];
$numRotation = $statInfo[$setNum]['totalCombos'];
// Calculate the number of times we want to display each value
$numDisplayed = $totalCombos / $numRotation;
// Set our display counter to 1
$displayCounter = 1;
// Loop X times where X is the total number of possible combinations
// so we can generate our result set. This unorthodox method worked
// better for me than doing nested functions or other types of recursion.
for($j=0; $j<$totalCombos; $j++) {
// Get the value in the current set at the specified location
$value = $arraySet[$setNum][$key];
// If we're on our last set, don't add a dash
if($setNum < $numSets-1) $value .= "-";
// Add the value we retrieved to our result set in this loop
$results[$j] .= $value;
// If we've displayed the current value the correct number of times,
// go to the next value and reset the counter
if($displayCounter == $numDisplayed) {
$key++;
$displayCounter = 0;
// If we've reached the end of our values in this set, let's reset
// it back to the beginning so we can loop through them again
if($key >= $numVars) $key = 0;
}
// Increment the counter, obviously
$displayCounter++;
}
// Reset the key to 0 as we'll be starting with a new set of values
$key = 0;
}
// Return the results back to the script. This returns an ARRAY.
return $results;
}
/******************************************************************/
/************************** END FUNCTION **************************/
/******************************************************************/
// Some sample data, but you can feed the $arraySet variable with any
// array data you wish (i.e. from a DB or filesystem).
$arraySet[] = array('a','b','c') ;
$arraySet[] = array('1','2','3','4','5');
$arraySet[] = array('x','y','z');
$arraySet[] = array('A','B');
// Build the set of combinations from any number of arrays and values. It's
// worthwhile to note that $arraySet is an array of arrays. This function does
// NOT build out permutations of a string or any other such data.
$resultsArray = buildPermutationResult($arraySet);
// Print out our results by imploding the array we got back.
echo "Results: <br />";
echo implode("<br />", $resultsArray);
?>