2

My knowledge in PHP and MySQL is not so deep. I have a table with pick_number (varchar,4), field. I need 3 things:

1)Combine the 4 numbers and generate all possible combinations (will be 24 if all numbers are different) Numbers must have 4 numbers each, like: 1234, 2341,etc.... Reduces the number of combinations if one number is the same of another, like 1123. Here i thought to isolate the numbers:

$pick_number=GetRow("SELECT pick_number from table");
$n1=substr($pick_number, 0, 1);
$n2=substr($pick_number, 1, 1);
$n3=substr($pick_number, 2, 1);
$n4=substr($pick_number, 3, 1);

2)Count the combinations and store in another field;

3)Store the combinations in another field, so i can search if i need(i guess comma separeted should do the trick).

Thanks!

noufalcep
  • 3,446
  • 15
  • 33
  • 51
nmj77
  • 21
  • 2

2 Answers2

0

Try this one.

 function pc_permute($items, $perms = array( )) {
    if (empty($items)) { 
        print join(' ', $perms) . "\n";
    }  else {
        for ($i = count($items) - 1; $i >= 0; --$i) {
             $newitems = $items;
             $newperms = $perms;
             list($foo) = array_splice($newitems, $i, 1);
             array_unshift($newperms, $foo);
             pc_permute($newitems, $newperms);
             echo '<br>';
         }
    }
}

    pc_permute(array(0,1,2,3));
Salini L
  • 829
  • 5
  • 15
  • 43
  • Hi, returned duplicated numbers when one is the same, as 7771, should return only 1777,7177,7717,7771, but returned 24 numbers, most of them are the same. – nmj77 Sep 20 '14 at 12:25
  • If u store it to an array then u can remove the duplicates from array using "array_unique($array)" (http://php.net/manual/en/function.array-unique.php) – Salini L Sep 22 '14 at 05:50
0

For combining all posible combinations you can use this function:

https://stackoverflow.com/a/19067884/4015178

Reduces the number of combinations:

foreach($combinations as $key => $combination) {
    foreach (count_chars($combination, 1) as $val) {
        if($val > 1) {
             unset($combinations[$key]);
             break;
        }
    }
}

More about count_chars() - http://php.net/manual/en/function.count-chars.php

Community
  • 1
  • 1
  • Hi, returned all 255 possibilities, but, should only combine the strings without duplicate them, the number was 1234, the output should scramble all possibilities os this four numbers, without duplicated, but returned 2222, 2223, etc... thx – nmj77 Sep 20 '14 at 12:35