I am trying to create a function to generate all the possible combinations of a particular group of characters, and have it variable based on length.
I have a function to create an array of the characters I would like, and this works fine.
function generate_characters($l, $n, $d) {
$r = array();
if ($l === true) {
foreach (range('a', 'z') as $index) {
array_push($r, $index);
}
}
if ($n === true) { array_push($r, '0','1','2','3','4','5','6','7','8','9'); }
if ($d === true) { array_push($r, '-'); }
return $r;
}
I then need to have it create an array of all possible combinations based on $length, for example if '$length = 1' I need the following array
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
[6] => g
[7] => h
[8] => i
[9] => j
[10] => k
[11] => l
[12] => m
[13] => n
[14] => o
[15] => p
[.... removed some values to save on length ....]
[35] => 9
)
but if '$length = 2' I need this
Array
(
[0] => aa
[1] => ab
[2] => ac
[3] => ad
[4] => ae
[5] => af
[6] => ag
[7] => ah
[8] => ai
[9] => aj
[.... removed some values to save on length ....]
[1329] => 97
[1330] => 98
[1331] => 99
)
I have tried array_walk() and array_walk_recursive(), along with several foreach and while loops, to no avail.
I can get it to work by manually doing it for each length, but not with a variable length by doing this, but don't know how to make it variable by length.
function generate_two($l, $n, $d) {
$r = array();
foreach (generate_characters($l, $n, false) as $v1) {
foreach (generate_characters($l, $n, $d) as $v2) {
array_push($results, "$v1$v2");
}
}
return $r;
}
all this whilst, not having the '-' as the first character, although I could remove those values after generating the array if I needed to.
Thanks, Dan