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?
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?
 
    
     
    
    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
 
    
    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;
        }
 
    
    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
