PHP基于数组长度的循环N次

I have this array :

$products = array('0', '1', '2', '3');

and I want to have this combination :

0
0,1
0,1,2
0,1,2,3 // my code can't show this result
0,1,3 // my code can't show this result
0,2
0,2,3
0,3
1
1,2
1,2,3
1,3
2
2,3
3

and here's my code :

$rows = count($products);
for ($i = 0; $i < $rows; $i++) {

    echo $i . '<br>';

    for ($j = $i + 1; $j < $rows; $j++){
        echo $i . $j . '<br>';

        if (!empty($products[$j+1])) {
            echo $i . $j . $products[$j+1] . '<br>';
        }

    }

}

but, unfortunately I missed 2 results :

0,1,2,3
0,1,3

and I still have doubt about this for loop inside loop, while the number of combination is determined by array length.

how to get that missed combination and perfectly works for different array length?

Try this

    <?php
        $set = array('0', '1', '2','3');
        $power_set = possible_set($set);
        echo "<pre>";
        print_r($power_set);
        echo "</pre>";
        function possible_set($array) {
            $results = array(array( ));
            foreach ($array as $element)
                foreach ($results as $inner_element)
                    array_push($results, array_merge(array($element), $inner_element));

            unset($results[0]);
            $results = array_values($results);
            return $results;
        }
    ?>

https://eval.in/516963

$products = array('0', '1', '2', '3');

    $rows = count($products);  //missing here array count......

    for ($i = 0; $i < $rows; $i++) {

        echo $i . '<br>';

        for ($j = $i + 1; $j < $rows; $j++){
            echo $i . $j . '<br>';

            if (!empty($products[$j+1])) {
                echo $i . $j . $products[$j+1] . '<br>';
            }

        }

    }