3

I have a array of number:

$numbers = array(1,2,3);

The order does not mean anything. If the numbers given were 1, 2 and 3, then I would want to receive this as a result:

1
2
3
1 2
1 3
2 3
1 2 3

How can I achieve that?

dWinder
  • 11,597
  • 3
  • 24
  • 39
Adam Skiba
  • 633
  • 1
  • 6
  • 15
  • What have you tried so far? please add your code to the question – Professor Abronsius May 31 '19 at 09:20
  • I have not tried anything yet. Not sure where to start besides writing out a function but if there is a ready made function out there already, I'd rather use that. – Adam Skiba May 31 '19 at 09:21
  • https://stackoverflow.com/questions/10834393/php-how-to-get-all-possible-combinations-of-1d-array This solution is close to what I need, but without the extra combinations with different orders. – Adam Skiba May 31 '19 at 09:28
  • already resolved here: https://stackoverflow.com/questions/2617055/how-to-generate-all-permutations-of-a-string-in-php/2617080#2617080 – 4givN May 31 '19 at 09:31
  • https://stackoverflow.com/questions/5162254/all-possible-combinations-of-an-array – Kmg Kumar May 31 '19 at 09:40
  • @kmgkumar this link to the solution in JAVA -> I guess OP need PHP – dWinder May 31 '19 at 09:51

3 Answers3

3

You can use the following recursion function:

function powerSet($arr) {
    if (!$arr) return array([]);
    $firstElement = array_shift($arr);
    $recursionCombination = powerSet($arr);
    $currentResult = [];
    foreach($recursionCombination as $comb) {
        $currentResult[] = array_merge($comb, [$firstElement]);
    }
    return array_merge($currentResult, $recursionCombination );
}

Now print_r(powerSet([1,2,3])); will give you all those option as arrays.

Edited with adding option of empty array as powerSet

dWinder
  • 11,597
  • 3
  • 24
  • 39
0

Partially solved with:

PHP: How to get all possible combinations of 1D array?

Then added my own function to clean it up:

        function clean_depth_picker(&$result) {
                $results = array();
                foreach($result as $value) {
                        if ( substr_count($value, " ") == 0 ) {
                                $results[] = $value;
                        } else {
                                $arr = explode(" ", $value);
                                sort($arr);
                                $key = implode(" ", $arr);
                                if ( !in_array($key, $results) )
                                        $results[] = $key;
                        }
                }
                $result = $results;
        }
Adam Skiba
  • 633
  • 1
  • 6
  • 15
0

May be I found it on a old SO post or Github gist.

<?php
function uniqueCombination($in, $minLength = 1, $max = 2000) {
    $count = count($in);
    $members = pow(2, $count);
    $return = array();
    for($i = 0; $i < $members; $i ++) {
        $b = sprintf("%0" . $count . "b", $i);
        $out = array();
        for($j = 0; $j < $count; $j ++) {
            $b{$j} == '1' and $out[] = $in[$j];
        }
        count($out) >= $minLength && count($out) <= $max and $return[] = $out;
        }
    return $return;
}

$numbers = array(1,2,3);
$return = uniqueCombination($numbers);
sort($return);
print_r(array_map(function($v){ return implode(" ", $v); }, $return));
?>

Output:

Array (
   [0] => 1 
   [1] => 2 
   [2] => 3 
   [3] => 1 2 
   [4] => 1 3 
   [5] => 2 3 
   [6] => 1 2 3 
)

DEMO: https://3v4l.org/lec2F

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103