1

My permutation/combinations of data

$combinations = [[]];
$data = [

        ['alteration1', 'alteration2', 'alteration3', 'alteration4' ... upto 5 each],
        ['alteration1', 'alteration5', 'alteration6', 'alteration7' ... upto 5 each],
        ['alteration8', 'alteration9', 'alteration10', 'alteration5' ... upto 5 each],
        ... upto 6 max
    ];

    $length = count($data);

    for ($count = 0; $count < $length; $count++) {
        $tmp = [];
        foreach ($combinations as $v1) {
            foreach ($data[$count] as $v2)
                $tmp[] = array_merge($v1, [$v2]);

        }
        $combinations = $tmp;
    }

  print_r($combinations);

The script would generate such sets

 0 => array:3 [▼
    0 => "alteration1"
    1 => "alteration1"
    2 => "alteration8"
  ]
  1 => array:3 [▼
    0 => "alteration1"
    1 => "alteration1"
    2 => "alteration9"
  ]

... the issue begins when I start getting the same sequences of data in my case array index 0 and 20 would be exactly the same despite any position.

 20 => array:3 [▼
        0 => "alteration8"
        1 => "alteration1"
        2 => "alteration1"
      ]
      21 => array:3 [▼
        0 => "alteration1"
        1 => "alteration9"
        2 => "alteration1"
      ]
 $final = []; // remove duplicates

The basic idea is to keep $combinations array's unique (alteration1,alteration2,alteration3) is equal to (alteration3,alteration1,alteration2) therfore it should be skipped in the $final array. I haven't really found anything around SO and google. Thanks for the help. $data dimentions [ from 1 - 6 ], each array inside can be [ 1 - 6 ]. Following script might not be working as expected .

http://sandbox.onlinephpfunctions.com/code/3ad5084386c2185f7619aaac152b638873039ee8

2
  • What could be possible duplicates? Commented Nov 7, 2019 at 13:39
  • (alteration1,alteration2,alteration3) is equal to (alteration3,alteration1,alteration2) f.e any same combination that repeats, ABC = BCA = CBA to simplify should check upto 6 elements ( and return only 1 ) position doesn't matter Commented Nov 7, 2019 at 13:45

1 Answer 1

1
  • We iterate over the data and find unique elements first for each set using array_unique.

  • We then natsort them to get a sorted form and serialize them using implode(). By doing this, we would get the same serialized code for sets ABC,CBA,BAC etc.

  • We then find duplicates using keys check inside a variable, say $set. If the serialized key is set, we exclude it from the results, else we include it in our final results.

Snippet:

<?php

$data = [
    ['alteration1', 'alteration4',],
    ['alteration2','alteration3'],
    ['alteration2','alteration3'],
    []
];


$combinations = [[]];

foreach($data as $index => $current_data){
   $current_data = array_unique($current_data);   
   if(empty($current_data)) continue; 
   $temp_combinations = [];
   foreach($current_data as $value){
       foreach($combinations as $each_combination){
           $temp_combinations[] = array_merge($each_combination,[$value]);
       }
   }

   $combinations = $temp_combinations;

}

$set = [];
$unique_combinations = [];

foreach($combinations as $each_combination){
    natsort($each_combination);
    $serialized_form = implode(",",$each_combination);
    if(isset($set[$serialized_form])) continue;
    if(empty($each_combination)) continue;
    $unique_combinations[] = $each_combination;
    $set[$serialized_form] = true;
}

print_r($unique_combinations);

Demo: https://3v4l.org/Do6oH

Sign up to request clarification or add additional context in comments.

6 Comments

what if an array is empty, that might also be a case, else its working
@KavvsonEmpcraft wait, there's a bug. I am fixing.
@KavvsonEmpcraft I have updated my answer. I have also added a test data set in the code where it prints the combination of 1,2,3 only once and so goes with others. Initially, it was giving 1,3,2 also along with 1,2,3.
@KavvsonEmpcraft Added a check for empty data as well.
3v4l.org/F8NNH consider that line 7, that's what I meant
|

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.