I have a PHP page with two variables: $nbRank
and $nbNumeric
. Depending of these two variables, I want to generate an array containing all combinations existing. For example:
If $nbRank = 3
and $nbNumeric = 2
I would have:
0 0 0
0 0 1
0 0 2
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 2 2
1 0 0
1 0 1
1 0 2
1 1 0
1 1 1
1 1 2
1 2 0
1 2 1
1 2 2
2 0 0
2 0 1
2 0 2
2 1 0
2 1 1
2 1 2
2 2 0
2 2 1
2 2 2
So, I create different loop and formulas to get the final result, but it doesn't works. This is what I did :
$result = array();
$nbIntRank = 0;
$nbIntNumeric = 0;
$nbRank = array();
$nbNumeric = array();
$nb_rangs = 3;
$nb_chiffres = 2;
for ($i = 1; $i <= $nb_rangs; $i++){
$nbRank[$i] = 0;
}
$nbIntRank = count($nbRank);
for ($i = 0; $i <= $nb_chiffres; $i++){
$nbNumeric[$i] = $i;
}
$nbIntNumeric = count($nbNumeric);
$algo = ($nb_rangs * ($nb_chiffres + 1)) * ($nb_rangs * ($nb_chiffres + 1));
$nbLine = $algo / ($nb_rangs);
$occ = 0;
for ($i = 0; $i < $nbLine; $i++){
foreach ($nbRank as $nbrItem => $nbrValue){
$result[$i][] = $nbrValue;
$occ++;
}
}
echo '#############<br />';
echo '### DATAS ###<br />';
echo '#############<br /><br />';
echo '- Nb Elements : '.$algo.'<br />';
echo '- Nb Lines : '.$nbLine.'<br />';
echo '- Nb Valuable Occurency : '.$occ.'<br />';
echo '<br /><hr /><br />';
echo '##############<br />';
echo '### PARSER ###<br />';
echo '##############<br /><br />';
echo '<pre>';
var_dump($result);
echo '</pre>';
I managed to create my final array with empty values (81 values, in 27 lines of 3 elements) but it only contains 0.
Here's a recursive solution:
Simple idea: What you want is every number from 0 to X with base
$nbNumeric
, thus we just convert the maximum number to base 10, iterate over it with the common 10-based operators, and convert it back to base$nbNumeric
again.Probably more readable, but in fact exactly the same
You indicated you'll be fine with pseudo-code.. Sorry I cannot offer specific correction for your php code [if these answers appear - they might be more educating], but I'd chose a recursive solution for this problem.
In each level of the recursion, try all possibilities, and call the same function to find all combinations of one smaller size.
Pseudo-Code:
Invoke with
findCombinations(3,3,[],resultList)
where[]
is just empty list, andresultList
will hold the list of combination when the algorithm is done. this invokation will get all combinations of size 3 with elemenets 0,1,2.Complexity note: The number of possibilities is growing exponentially [O(rangesize)], so if you try to invoke it with 20,20 for instance - it might take some [very long] time, for any solution.