I have an array. This an example:
$array = ['A', 'B', 'C', 'D']
I would like all possible combination to get the following result:
$new_array = [
['A', 'B'], ['A', 'C'], ['A', 'D'],
['A', 'B', 'C'], ['A', 'B', 'D'], ['A', 'C', 'D'],
['B', 'C'], ['B', 'D'],
['B', 'C', 'D']
['C', 'D']
]
So, as you can see, the result is a combination of array but they are all uniques. I mean these 2 array [‘A’, ‘B’] and [‘B’, ‘A’] is not wanted. And values in arrays need to be sorted in alphanumeric order.
Unless I’m wrong, an array with 4 values gives 10 possibles and uniques combinations.
Thanks for your help.
2
Answers
You need a
power set
of the given elements with the additional filters of removing individual elements and the entire set as a whole.So, for a collection of
4
elements, total15
combinations are possible. For your case, we will have to remove4
individual elements and the whole set itself making it10
different sets or combinations.For every integer from
1
to14
, we keep including all the elements whose index comes in the binary representation of the current integer in iteration and add them to the result. If any integer has only 1 bit set(meaning being a power of 2), it means it is an individual element and we simplycontinue
our loop skipping the processing part.Finally, we use
usort
to sort the sets alphabetically and usestrcmp
to compare 2 different sets by converting them to strings usingimplode
separated by|
character to avoid any overlapping issues.Snippet:
Online Demo