There is an array $test = ['a','b','c','d'];

As a result, it is necessary to obtain possible combinations of the given array, without pavtora (a, b | b, a) in the necessary order;

 a b c d a,b a,c a,d b,c b,d c,d a,b,c a,b,d .... 

I can not figure out how to implement it. I tried this way.

 $test = ['a','b','c','d']; $new = []; foreach ($test as $value) { $new[] = $value; foreach ($test as $value1) { if($value != $value1) { // $new[] = $value; $new[] = $value.','.$value1; } } } function sort_value_strlen($a, $b) { return mb_strlen($a) - mb_strlen($b); } uasort($new, 'sort_value_strlen'); print_r($new); 

But it does not work as I need. Result

 Array ( [0] => a [4] => b [8] => c [12] => d [1] => a,b [2] => a,c [3] => a,d [5] => b,a [6] => b,c [7] => b,d [9] => c,a [10] => c,b [11] => c,d [13] => d,a [14] => d,b [15] => d,c ) 

It does not go more than 2 values ​​+ there are repetitions (a, b | b, a)

And is it possible to implement without using functions?

    3 answers 3

    Recursive Python algorithm:

     def gen(lst, idx, result): if idx < 0: if len(result)> 0: print(result) else: gen(lst, idx - 1, result) gen(lst, idx - 1, lst[idx] + " " + result) gen(["a","b","c"], 2, "") ababcacbcabc 

    PHP:

     $AA = array('a','b','c'); $BB = array(); function gen($A, $B, $idx) { if ($idx < 0) { if(sizeof($B)>0) print_r($B); return; } gen($A, $B, $idx - 1); array_unshift($B, $A[$idx]); gen($A, $B, $idx - 1); return; } gen($AA, $BB, sizeof($AA)-1); 

    Non-recursive algorithm - we loop through the numbers from 1 to 2^N-1 , where N is the length of the array, and represent the loop counter in binary form. The k-th bit is set - the k-th element of the array is present in the combination

     L = ["a","b","c", "d"] for i in range(1, 1<<len(L)): t = i k = 0 result = "" while t > 0: if t & 1: result += L[k] t >>= 1 k += 1 print(result) >> ab ab c ac bc abc d ad bd abd cd acd bcd abcd 
    • under the condition of the vehicle was said about the required order of withdrawal. @nicolaa, the output order is not important? - Jigius
    • @Jigius Then the easiest way is to add the results into an array of key-length, then in order of the keys to display. Separate generation of combinations of each length is not very profitable. - MBo
    • @MBo please tell me how the result in an array? I try to set an empty array at the beginning of the function $test = array(); , then instead of print_r($B); I am adding to the array $test[] = $B; and output it to return $test; and call the function in print_r() but it is empty on the screen - nicolaa
    • @nicolaa I’m not going to tell you here, I don’t know PHP. In another language, I would make a global two-dimensional array, a list, or a dictionary $C , and put it into something like this $C[sizeof($B)-1][] = $B; - MBo 1:51 pm
    • If PHP has a stable sort, then you can just sort the ready list by length (a stable order will not spoil an existing order - for example, Insertion Sort or Merge) - MBo

    this is a recursion task

      $test = ['a','b','c','d']; function fillArray($incomeArr) { $outcomeArr = []; $firstLetter = $incomeArr[0]; $incomeArr = array_slice($incomeArr, 1); for( $i = 0; $i < sizeof($incomeArr); $i++ ) { $outcomeArr[] = $firstLetter . ',' . $incomeArr[$i]; } if (sizeof($incomeArr) > 1) { $outcomeArr = array_merge($outcomeArr, fillArray($incomeArr)); } return $outcomeArr; } var_dump( fillArray($test) ); 

    the idea is that a is concatenated first with b, c, d. Then take b and concatenate with, d. Finally, take with + d

    • Thank you, but how can I continue to sort out the entire array? What was a result of a, b, c, d? - nicolaa
    • for ($ i = 0; $ i <3; $ i ++) {$ test = array_merge ($ test, fillArray ($ test))} - Sergey Konovalov

    Another option with recursion:

     <?php function tuples(array $arr, array &$res, array $prefix = [], $offset = 0) { for ($i = $offset; $i < count($arr); $i++) { $nextPrfx = array_merge($prefix, [$arr[$i]]); array_push($res, $nextPrfx); tuples($arr, $res, $nextPrfx, ++$offset); } } $result = []; tuples(['a', 'b', 'c', 'd'], $result); var_dump($result); 

    Sandbox