1
Je veux générer toutes les combinaisons de longueur r à partir d'un ensemble [0 ... (n-1)]tableau php combinaison
Ainsi, la sortie doit être comme suit (n = 6 r = 2)
$res = array(array(0,1),array(0,2),array(0,3),array(0,4),array(0,5),array(1,2),array(1,3),array(1,4),array(1,5),array(2,3),array(2,4),array(2,5),array(3,4),array(3,5),array(4,5));
avec une fonction comme
function permutate($select, $max)
où sélectionnez $ = r et $ max = n
c'est ma tentative actuelle, mais mon cerveau d oesnt semblent fonctionner ce soir et il ne fonctionne que pour $ select = 2
function permutate($select, $max)
{
$out = array();
for($i = 0; $i < ($max) ; $i++)
{
for ($x = ($i + 1); $x < ($max); $x++)
{
$temp = array($i);
for($l = 0; $l < ($select-1); $l++)
{
if(($x+$l) < $max)
{
array_push($temp, $x+$l);
}
}
if(count($temp) == $select)
{
array_push($out, $temp);
}
}
}
return $out;
}
Merci à l'avance
cela ne fonctionne que pour $ select = 2, comme quand $ select = 3 vous obtenez seulement tableau (tableau (0,1), tableau (0,2) ......); quand il devrait être tableau (array (0,1,2), tableau (0,2,3) ......) – lloydsparkes
Oh, maintenant j'ai votre idée! Fixer la réponse ... – soulmerge
oui fonctionne parfaitement grâce – lloydsparkes