SUGGESTION: Separate your array into different sets, that can make them more manageable, here is a similar discussion
Creating tuples based on sets...
Cartesian Cross-Product gives a good orientation too...
perhaps you would wanna consider Set::CrossProduct as an OOP approach or read Using Nested Iterators to find a Cross Product...
Excellence is an Endeavor of Persistence.
Chance Favors a Prepared Mind.