-1

Note: This question may already have an answer, but I probably would not have understood it if Dan D. had not answered me, as he did, precisely and clearly. So thanks again to him.


I would like to get the following arrays in PHP. The pattern is simple: the arrays have a size of from 2 to x, and each element is greater than the previous one. I can do it easily with several loops for a given x, but I would like to do it in general for any x. This seems a little tricky and I can not do it. Can someone help me?

For x = 2:

[1,2]


For x = 3:

[1,2]
[1,3]
[2,3]
[1,2,3]


For x = 4:

[1,2]
[1,3]
[1,4]
[2,3]
[2,4]
[3,4]
[1,2,3]
[1,2,4]
[1,3,4]
[2,3,4]
[1,2,3,4]


For x = 5:

[1,2]
[1,3]
[1,4]
[1,5]
[2,3]
[2,4]
[2,5]
[3,4]
[3,5]
[4,5]
[1,2,3]
[1,2,4]
[1,2,5]
[1,3,5]
[1,4,5]
[2,3,4]
[2,3,5]
[2,4,5]
[3,4,5]
[1,2,3,4]
[1,2,3,5]
[1,2,4,5]
[1,3,4,5]
[2,3,4,5]
[1,2,3,4,5]


For x = 6:

[1,2]
[1,3]
[1,4]
[1,5]
[1,6]
[2,3]
[2,4]
[2,5]
[2,6]
[3,4]
[3,5]
[3,6]
[4,5]
[4,6]
[5,6]
[1,2,3]
[1,2,4]
[1,2,5]
[1,2,6]
[1,3,5]
[1,3,6]
[1,4,5]
[1,4,6]
[1,5,6]
[2,3,4]
[2,3,5]
[2,3,6]
[2,4,5]
[2,4,6]
[2,5,6]
[3,4,5]
[3,4,6]
[3,5,6]
[4,5,6]
[1,2,3,4]
[1,2,3,5]
[1,2,3,6]
[1,2,4,5]
[1,2,4,6]
[1,2,5,6]
[1,3,4,5]
[1,3,4,6]
[1,3,5,6]
[1,4,5,6]
[2,3,4,5]
[2,3,4,6]
[2,3,5,6]
[2,4,5,6]
[3,4,5,6]
[1,2,3,4,5]
[1,2,3,4,6]
[1,2,3,5,6]
[1,2,4,5,6]
[1,3,4,5,6]
[2,3,4,5,6]
[1,2,3,4,5,6]


Etc.
  • Possible duplicate of [Finding the subsets of an array in PHP](https://stackoverflow.com/questions/6092781/finding-the-subsets-of-an-array-in-php) – Progman Dec 02 '18 at 08:45

1 Answers1

0

So given the power set function in this answer, with very slight adjustments you can have the desired outcome

function powerSet($n, $minLength=2) {
    $in = range(1,$n);
    $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++) {
            if ($b{$j} == '1') $out[] = $in[$j];
        }
        if (count($out) >= 2) {
            $return[] = $out;
        }
    }
    sort($return);
    return $return;
}

Live demo here: https://3v4l.org/kIKrU

Dan D.
  • 815
  • 9
  • 16
  • Dan, this code does exactly what I need. Thank you very much for the code and the demo. – Dominique Bauer Dec 03 '18 at 04:18
  • @DominiqueBauer, you're welcome, you can mark it an an answer then – Dan D. Dec 03 '18 at 11:23
  • Dan, This is my first time on Stack Overflow and I am not sure how it works. When I click on "This answer is useful", the following message is displayed: "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score". Please tell me what I can do. – Dominique Bauer Dec 03 '18 at 20:58
  • At the top of my question, it says "This question may already have an answer here:, etc.". Well maybe, but I probably would not have understood if you had not answered me, as you did, precisely and clearly. So thank you very much again. – Dominique Bauer Dec 03 '18 at 20:59
  • @DominiqueBauer ah ok, no problem, glad it helped :) – Dan D. Dec 04 '18 at 06:40