Mrsoh Mrsoh - 2 months ago 6
PHP Question

php 4digit number repeat generate itself to all same num group

php 4digit number repeat generate itself to all same num group
eg my number is 1221
the group will be max 6group (each group not same number until max)
1221 1212
1122 2211
2121 2112

if 1444 will be max 4group
1444 4441 4414 4144

generate other group number all in 1221 ,no other extra num same as other number

other maybe 12group (eg 4522 )
and max is 24group (eg 6897 )

if 4444 = 1group

so is 4,6,12,24 group only

all results return to an array, array("1221","1212",...)

in php,how can do this function xxx() ?

Answer
$input = "1444";

function generate_num($prefix, $characters, &$permutations)
{   
 if (count($characters) == 1)    

$permutations[] = $prefix . array_pop($characters);
else
 {

    for ($i = 0; $i < count($characters); $i++)
    {
        $tmp = $characters;
        unset($tmp[$i]);

        generate_num($prefix . $characters[$i], array_values($tmp), $permutations);
    }
}
}
$characters = array();

for ($i = 0; $i < strlen($input); $i++)
$characters[] = $input[$i];

$permutations = array();

generate_num("", $characters, $permutations);

echo '<pre>'.print_r(array_unique($permutations),1).'</pre>';

result

    Array( [0] => 1444 [6] => 4144 [8] => 4414  [9] => 4441)