Create combinations with a specific quantity in a given set of numbers

Asked

Viewed 98 times

-1

So, I’m new to php and I couldn’t, after many attempts and in different ways, create something like.

I enter a value for the amount of numbers I will create in each combination in a given set of numbers.

I want for example, combinations with 5 numbers of this set below; Set [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40]

... I really need a light here.... I’ve already lost more than 2 days creating shapes and nothing. I tried to do with recursive calls but from 3 numbers generates error....

<?php
$qntdElementos = 5;
$tipoConjunto = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40];
$max = max($tipoConjunto);
//        var_dump($max);
$maxPrimeiraPosicao = max(array_keys($tipoConjunto))-($qntdElementos-1);
$limiteMaxTipoConjunto = max(array_keys($tipoConjunto));
$limiteMinTipoConjunto = min(array_keys($tipoConjunto));
$posicaoInicial = $qntdElementos-1;
$novaPosicaoMudanca = $posicaoInicial-1;
$arrayMutavel=[];
$combinacoes=[];
$strMutavel = "";
for ($a=$limiteMinTipoConjunto;$a<=$posicaoInicial;$a++){
    $strMutavel.="$a ";
    $arrayPosicoes[]=$a;
}
$maxPosicoes = max(array_keys($arrayPosicoes));
$arrayMutavel = explode(" ",trim($strMutavel));
var_dump($arrayMutavel, $maxPosicoes);
$c=-1;
$cp=$maxPosicoes+1;
do{
    $cp--;
    var_dump($cp);
    $vp = -1; //Valida a posição inicial
    $mp = $arrayPosicoes[$cp]; // Modificador de posição
    do {
        $c++;
        $vp++;

        var_dump($mp, $arrayMutavel);
        $vp>0?$pos = ($posicaoInicial + ($vp - 1)):$pos = $mp;
        $strMutavel = substr_replace($strMutavel,($posicaoInicial+$vp),strpos($strMutavel,"$pos"));
        $arrayMutavel = explode(" ",trim($strMutavel));
        for ($a = 0; $a <= $qntdElementos - 1; $a++) {
            if (isset($tipoConjunto[$arrayMutavel[$a]])) {
                $combinacoes[$c][] = $tipoConjunto[$arrayMutavel[$a]];
            }elseif (count($combinacoes[$c])<$qntdElementos){
                unset($combinacoes[$c]);
            }
        }
    }while($c<$limiteMaxTipoConjunto-1);
    echo "Saindo do primeiro loop = $strMutavel";
    $arrayMutavel = explode(" ",trim($strMutavel));
    $tmpArrayMutavel = $arrayMutavel[$cp-1];
    $tmpStrMutavel = $pos+1;
    $strMutavel = substr_replace($strMutavel,$tmpArrayMutavel+1,strpos($strMutavel,"$tmpArrayMutavel"));
    var_dump($strMutavel, $tmpArrayMutavel);
}

while($cp>0);



return $this->combinacoes = $combinacoes;

The return has been;

C:\xampp\htdocs\lotofacil\source\estrategias\estrategias.php:88:
array (size=5)
  0 => string '0' (length=1)
  1 => string '1' (length=1)
  2 => string '2' (length=1)
  3 => string '3' (length=1)
  4 => string '4' (length=1)
C:\xampp\htdocs\lotofacil\source\estrategias\estrategias.php:88:int 4
C:\xampp\htdocs\lotofacil\source\estrategias\estrategias.php:88:
array (size=5)
  0 => string '0' (length=1)
  1 => string '1' (length=1)
  2 => string '2' (length=1)
  3 => string '3' (length=1)
  4 => string '5' (length=1)

Until the error occurs;

C:\xampp\htdocs\lotofacil\source\estrategias\estrategias.php:88:
array (size=5)
  0 => string '0' (length=1)
  1 => string '1' (length=1)
  2 => string '2' (length=1)
  3 => string '3' (length=1)
  4 => string '22' (length=2)

( ! ) Notice: Undefined offset: 1 in C:\xampp\htdocs\lotofacil\source\estrategias\estrategias.php on line 93
Call Stack
#   Time    Memory  Function    Location
1   0.0001  418400  {main}( )   ...\estrategias.php:0
2   1.2059  8063776 source\estrategias\estrategias->geraCombinacoes( )  ...\estrategias.php:18

3 answers

0

  • The idea is not to order Felipe but to create the combinations but, still, grateful.

0

You can use the function array_rand to do this

  $qtd = 5;
  $input = array(1,2,3,4,5,6,7,8,9,10,11,12);
  $rand_keys = array_rand($input, $qtd);
  for($x=0;$x<$qtd;$x++){
     echo $input[$rand_keys[$x]] . "<br>";
  }

Or to draw X random numbers between a sequence, you can use this function:

function numeros_loteria($min, $max, $qtd) {
    $numeros = range($min, $max);
    shuffle($numeros);
    return array_slice($numeros , 0, $qtd);
}

//1 jogo
var_dump(numeros_loteria(1,25,15));

//Vários jogos
$qtd_jogos = 50;
for($x=0;$x<$qtd_jogos;$x++){
    echo "<h3>JOGO ".($x+1).":</h3> ";
    var_dump(numeros_loteria(1,25,15));
    echo "<hr>";
}
  • Pedro, thanks for the answer but, I would like to try to arrange them in order; 1234,1245,1256... 1345,1356,1367... 1456,1457,1458... But his idea undoubtedly gave me a new north to follow...

-1

$qtd = 5;
$input = array(1,2,3,4,5,6,7,8,9,10,11,12); 
for($x=0;$x<$qtd;$x++){
   $r = randon(0,count($input));
   $rand[] = input[$r];
}
asort($rand);
foreach( $rand as $chave => $valor ){
    echo "$chave = $valor<br>";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.