Im trying to write algorithm to get all combinations and increment by one at a same time and get this
function generate($index){
$xxx = '';
$flag = 0;
$lengtchString = strlen($index);
for($y = 0; $y != $lengtchString; $y++) {
$xxx .= "$y";
}
while ($flag != $lengtchString )
{
for ($i = 0; $i<$lengtchString-1; $i++)
{
$temp = $xxx[$i];
$xxx[$i] = $xxx[$i+1];
$xxx[$i+1] = $temp;
echo $xxx."<br>";
}
$flag++;
}
}
generate('abc');
and output is
102
120
210
201
021
012
I need to get all combinations not only for 3
digits but also all combinations .
For example if I write 'abc
'... I need output like
102
120
210
201
021
012
256
874
569
236
254
028
and so on.... untill 789
in condition that digits wouldnt repeat...
my mind actually to be blown, cant get proper algorithm for that. thanks in advance
Check this link PHP algorithm to generate all combinations of a specific size from a single set
<?php
function sampling($chars, $size, $combinations = array()) {
$charsArray = str_split($chars);
# if it's the first iteration, the first set
# of combinations is the same as the set of characters
if (empty($combinations)) {
$combinations = $charsArray;
}
# we're done if we're at size 1
if ($size == 1) {
return $combinations;
}
# initialise array to put new values in
$new_combinations = array();
# loop through existing combinations and character set to create strings
foreach ($combinations as $combination) {
foreach ($charsArray as $char) {
$new_combinations[] = $combination . $char;
}
}
# call same function again for the next iteration
return sampling($chars, $size - 1, $new_combinations);
}
// example
$output = sampling("0123456789", 3);
print "<pre>";print_r($output);