2016-02-11 11:25:40

1

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 . '
';

for ($j = $i + 1; $j < $rows; $j++){

echo $i . $j . '
';

if (!empty($products[$j+1])) {

echo $i . $j . $products[$j+1] . '
';

}

}

}

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?

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐