-2

There is a set of ordered numbers , say $nums = array(3, 7, 10, 12, 20, 24, 26, 34, 37);

I want to get all the possible combinations of 6 numbers from the above array , and the order is from the lowest number to the greatest number; for example :

3, 7, 10, 12, 20, 24
3, 7, 10, 12, 20, 26
3, 7, 10, 12, 20, 34
3, 7, 10, 12, 20, 37
3, 7, 10, 12, 24, 26
3, 7, 10, 12, 24, 34
3, 7, 10, 12, 24, 37
3, 7, 10, 12, 26, 34
3, 7, 10, 12, 26, 37
3, 7, 10, 12, 34, 37
3, 7, 10, 20, 24, 26
3, 7, 10, 20, 24, 34
3, 7, 10, 20, 24, 37
3, 7, 10, 20, 26, 34
3, 7, 10, 20, 26, 37
3, 7, 10, 20, 34, 37
etc...
pheromix
  • 14,975
  • 22
  • 72
  • 138
  • You must know that it was downvoted because you haven't started anything. Can you get the UNSORTED combinations? If you had the unsorted combinations, could you sort them? – dcromley Jun 15 '18 at 15:11
  • I dont know how to achieve it ! I dont have any clue ! – pheromix Jun 15 '18 at 15:15
  • From the top of my head I'd probably start by working index-wise like [0,1,2,3,4,5] and then increment this as a "number" mod how many integers you have so it produces [0,1,2,3,4,6],...[0,1,2,3,4,8] and then [0,1,2,3,5,6] and so on. – mroman Jun 15 '18 at 15:24
  • This can be done with a pretty standard recursive algorithm. – Phylogenesis Jun 15 '18 at 15:47
  • Possible duplicate of [PHP array combinations](https://stackoverflow.com/questions/3742506/php-array-combinations) – Phylogenesis Jun 15 '18 at 15:53
  • Google `permutations` – NobbyNobbs Jun 15 '18 at 15:55
  • 1
    It's combinations, rather than permutations, as ordering does not matter. – Phylogenesis Jun 15 '18 at 15:55
  • @Phylogenesis - The OP is asking for "permutations" not "combinations", otherwise the OP would have included 3,7,10,12,24,20 in the output example. To the OP ... ordering does indeed matter. – Kuya Jun 13 '20 at 11:09
  • @Kuya You've got that the wrong way round. [Permutations](https://en.wikipedia.org/wiki/Permutation#k-permutations_of_n) consider {1, 2, 3} to be distinct from {1, 3, 2}. Combinations do not. – Phylogenesis Jun 18 '20 at 12:42

1 Answers1

0

Sound like you need to work out the factorial of the length of the array, work it out like this:

function factorial($num){
    $factorial = 1;
    for ($x=$num; $x>=1; $x--) 
    {
        $factorial = $factorial * $x;
    }
}

$result = factorial(count($nums));
Liam G
  • 459
  • 3
  • 11
  • it gives the number of combinations ! What I want is the combinations ! you dont understand my needs ! – pheromix Jun 15 '18 at 15:50
  • Okay, no need to be like that, your question is worded very poorly. I'm only trying to help – Liam G Jun 15 '18 at 15:51
  • please delete your answer – pheromix Jun 15 '18 at 15:51
  • 1
    What is your problem? I was about to edit it and try and help you, but now forget it! P.s. leaving the answer here you baby – Liam G Jun 15 '18 at 15:53
  • @LiamG - I am also looking for a solution to this problem. I have searched SO for hours. Some answers have come close, but they all inevitably output extraneous results. I would consider it a huge benefit to myself and to the SO community if you could go ahead and answer this question. Thanks – Kuya Jun 13 '20 at 11:12