Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP combinations of array elements

I want to generate all possible combination of array elements to fill a placeholder, the placeholder size could vary.

Let say I have array $a = array(3, 2, 9, 7) and placeholder size is 6. I want to generate something like the following:

3,3,3,3,3,3
2,3,3,3,3,3
2,2,3,3,3,3
...........
...........
7,7,7,7,7,9
7,7,7,7,7,7

However (2,3,3,3,3,3) would be considered the same as (3,2,3,3,3,3) so the later one doesn't count.

Could anyone point me to the right direction? I know there is Math_Combinatorics pear package, but that one is only applicable to placeholder size <= count($a).

Edit I am thinking that this one is similar to bits string combination though with different number base

like image 479
user78431 Avatar asked Nov 13 '22 05:11

user78431


1 Answers

I have no PHP source code for you but some sources that might help.

Some C code. Look at 2.1: http://www.aconnect.de/friends/editions/computer/combinatoricode_g.html

Delphi code: combination without repetition of N elements without use for..to..do

Wiki article here

like image 193
bitWorking Avatar answered Nov 15 '22 04:11

bitWorking