I
I
Issue2021-10-19 13:05:20
PHP
Issue, 2021-10-19 13:05:20

How to make a generator of all possible combinations of characters of a given length?

There are 3 variables:

$start = 4;
$end = 6;
$sumb = [a,b,c,d,e,f,1,2,3,4,5,6,7,8,9,0];

These variables must be passed to the combination generator, and the output should be all possible combinations of $sumb characters from $start to $end so that they do not repeat.
At this point, I'm stuck and don't know what to write next. because I think the algorithm is fundamentally wrong:

function generator($start, $end, $sumbols){
    $array = [];
    while ($start != $end){ // Пока длина не максимальная
      $pos = 0;
      $word = '';
      while ($pos != $start) {
        foreach ($sumbols as $sumb) {
          
        }
        $pos++;
      }
      $array[] = $word;
      $start++;
    }
    return $array;
  }
  $sumbols = explode('a,b,c,d,e,f,1,2,3,4,5,6,7,8,9,0');
  var_dump(generator(4, 6, $sumbols));

Please tell me what kind of bike is there, or how to assemble your own.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-10-19
@paulenot

$result = array_map(
    function ($x) {
        return str_pad(dechex($x), 4, '0', STR_PAD_LEFT);
    },
    range(0, 0xffffff, 1)
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question