php for 数组长度,PHP基于数组长度的循环N次 (PHP For Loop N Times Based On Array Length)
2016-02-11 11:25:401I have this array :$products = array('0', '1', '2', '3');and I want to have this combination :00,10,1,20,1,2,3 // my code can't show this result0,1,3 // my code can't show this res
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?
更多推荐

所有评论(0)