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!

1
  • Take a look at permutations. Commented Sep 20, 2014 at 11:35

2 Answers 2

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));
Sign up to request clarification or add additional context in comments.

2 Comments

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.
If u store it to an array then u can remove the duplicates from array using "array_unique($array)" (php.net/manual/en/function.array-unique.php)
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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.