> I have 6 arrays with data and would like to have all possible > combinations > Eg. > > $a1 = array(1,2); > $a2 = array(3,4); > $a3 = array(5,6); > $a4 = array(7,8); > $a5 = array(9,10); > $a6 = array(11,12); > > the result should then be a new array like this: > > [0] = 1,3,5,7,9,11 > [1] = 1,4,5,7,9,11 > [2] = 1,6,5,7,9,11 > etc.... > > I know I end up with A LOT, but I need it ;-)
I did somethig similar once. First, put everything into one array to have it more clearly adressed. It could be easier to help if you'd explain rules of construction sets of digits. For example is this one correct "3,1,5,4,7,9"? I it is, solution is quite simple. You will need second array (name it 'used[]') indicating which element is already used. First, reset used[] array. Then launch a function working like this: void look4solution(0) /* this "0" is 'counter', initially set this way to 0 */ { if (counter==6) put_answer_into_array(answer); /* answer is complete, so output it - but remember to put it into the new row of output array - this function doesn't care about it */ /* now, put into answer[counter] first not previously used element... and next... and so on... */ else for (i=1; i<=12; i++) if (!used[i]) { /* we're just using i'th element so lock it... */ used[i]=true; /* ...put into answer[]... */ answer[counter]=input[i]; /* ...use any unused on next position... */ look4solution(counter+1); /* ...and free it! */ used[i]=fase; } } I can't guaratee it is working code. I didn't test it but this idea is good - Delphi program worked well but unfotunatelly I haven't it's source. You should have output like this: 1,2,3,4,5,6 1,2,3,4,5,7 1,2,3,4,5,8 ... 1,2,3,4,5,12 1,2,3,4,6,5 1,2,3,4,6,7 1,2,3,4,6,8 and so on -- PHP Windows Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php