i need to make numbers permutations (with some features), but my .php and mysql is not so improved. The numbers will be posted by a .html form, max 10 numbers. It will permute the numbers to create unique thousand number:
if 4 different numbers is posted by the .html input (1,2,3,4): will return: 1234,1243,1324,1342... (24 unique combinations)
if 5 different numbers is posted by the .html input (1,2,3,4,5): will return: 1234,1235,1243,... (120 unique combinations)
The number will only repeat if it posted, as (1,1,2,4) will return: 1124, 1142, 1214,...(12 unique combinations)
Then i need to store the combinations into a mysql row, comma separated.
I tryed this function(found here Permutations - all possible sets of numbers):
$p = permutate(array('1','2','3'));
foreach($p as $perm)
print join("",$perm)."|\n";
function permutate($elements, $perm = array(), &$permArray = array())
{
if(empty($elements))
{
array_push($permArray,$perm); return;
}
for($i=0;$i<=count($elements)-1;$i++)
{
array_push($perm,$elements[$i]);
$tmp = $elements; array_splice($tmp,$i,1);
permutate($tmp,$perm,$permArray);
array_pop($perm);
}
return $permArray;
}
The output: 123| 132| 213| 231| 312| 321|
But when is given: $p = permutate(array('1','2','2'));
the output is: 122| 122| 212| 221| 212| 221| When should be 122|212|221
I tried array_unique, but did´t work, probally becouse i did it wrong.
Thx