0

I need to remove duplicates from a table row. to Combine the 3 numbers and generate all possible combinations, posted by .html form (will be 6 if all numbers are different) Numbers must have 6 numbers each, like: 123, 234,etc.... Reduces the number of combinations if one number is the same of another, like 112. What i did, till now... to isolate the numbers and store it in a row:

        $cc=GetRow("SELECT numbers FROM table");
        $n1=substr($cc, 0, 1);
        $n2=substr($cc, 1, 1);
        $n3=substr($cc, 2, 1);

        //scrambling the numbers
        $n1n2n3=$n1.$n2.$n3; //123 number stored
        $n1n3n2=$n1.$n3.$n2; //132 number stored
        $n2n1n3=$n2.$n1.$n3; //213 number stored
        $n2n3n1=$n2.$n3.$n1; //231 number stored
        $n3n1n2=$n3.$n1.$n2; //312 number stored
        $n3n2n1=$n3.$n2.$n1; //321 number stored

        $sql = sqlQuery("UPDATE table SET cc_concat = CONCAT_WS(',', '$n1n2n3', '$n1n3n2','$n2n1n3','$n2n3n1','$n3n1n2','$n3n2n1')");

        But here´s the problem:
        if the number is 112 will generate duplicates, only 3 are uniques:
        $n1n2n3=$n1.$n2.$n3; //112 number stored
        $n1n3n2=$n1.$n3.$n2; //121 number stored
        $n2n1n3=$n2.$n1.$n3; //112 number stored
        $n2n3n1=$n2.$n3.$n1; //121 number stored
        $n3n1n2=$n3.$n1.$n2; //211 number stored
        $n3n2n1=$n3.$n2.$n1; //211 number stored

        Is there a way to update the table without the duplicates? or remove the duplicates
        after update?

Thanks!

nmj77
  • 21
  • 2

1 Answers1

0

You could use an array to store the combinations and take advantage of the in_array() function to make sure you won't have duplicates:

$combinations = array();
if ( !in_array( $n1n2n3, $combinations ) ) $combinations[] = $n1n2n3;
if ( !in_array( $n1n3n2, $combinations ) ) $combinations[] = $n1n3n2;
// Do the same for the rest

// Then you could easily concatenate the result set from PHP.
$cc_concat = implode( ',', $combinations );

// Finally update the database.
$sql = sqlQuery("UPDATE table SET cc_concat = '{$cc_concat}' ");    

Something like that should solve the problem.

Additionally you might prefer to use an algorithm to generate the combinations for you from an array of the three digits. For such algorithm you can check this thread out: Get all permutations of a PHP array?

Community
  • 1
  • 1