You indicated you'll be fine with pseudo-code.. Sorry I cannot offer specific correction for your php code [if these answers appear - they might be more educating], but I'd chose a recursive solution for this problem.
In each level of the recursion, try all possibilities, and call the same function to find all combinations of one smaller size.
Pseudo-Code:
findCombinations(range,size,sol,resultList):
if (size ==0): #base clause
resultList.append(copy(sol)) #making a copy of sol and appending it as a solution
return
for each i in (0,range):
sol.append(i)
findCombinations(range,size-1,sol,resultList) #recursive invokation, with smaller size
sol.deleteLast() #clean up environment before next calls
Invoke with findCombinations(3,3,[],resultList)
where []
is just empty list, and resultList
will hold the list of combination when the algorithm is done. this invokation will get all combinations of size 3 with elemenets 0,1,2.
Complexity note:
The number of possibilities is growing exponentially [O(rangesize)], so if you try to invoke it with 20,20 for instance - it might take some [very long] time, for any solution.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…