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.
See Question&Answers more detail:os