Hi Daniel, Cheers for your input - and yeah I've done exactly that join to actually retrieve the various options, but in a nice concatenated format.
Cheers, Kirk 2009/4/1 Daniel Bush <[email protected]>: > This is way off-beam, but the other way you can get a cartesian product is > using a relational database. > Suppose you have a table for each option group eg colours and sizes > Then you go > select colours.*,sizes.* > from colours,sizes > and away you go. > Of course you can extend that to several more tables. If someone wants S / > Red or Red / S, you just add a where-clause to the above with the two > conditions using AND. > I find a use for this sort of join once every... uhm .. decade I suppose. > > -- > Daniel Bush > > http://blog.web17.com.au > http://github.com/danielbush > > > 2009/4/1 Torm3nt <[email protected]> >> >> God I LOVE you guys! lol >> >> That's the exact term I needed - cartesian product. Simple google >> search got me exactly what I needed. >> >> AHHH. I could hug you all! :D >> >> >> Kirk >> >> On Wed, Apr 1, 2009 at 4:12 PM, Xavier Shay <[email protected]> wrote: >> > >> > I solve exactly the same problem you're trying to with the below method >> > (cartesian product) >> > >> > There's a few implementations around the net, I'd be surprised if there >> > isn't one for PHP. >> > >> > Julian Doherty wrote: >> >> So you basically want a cartesian product of an arbitrary list of >> >> lists? >> >> >> >> The Ruby facets gem does >> >> that: >> >> http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD >> >> >> >> Heres what the Ruby code looks like (works for an arbitrary number of >> >> list arguments): >> >> >> >> class Array >> >> 2 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l2> >> >> >> >> 3 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l3> >> >> if RUBY_VERSION < '1.9' >> >> 4 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l4> >> >> >> >> 5 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l5> >> >> # Provides the cartesian product of two or more arrays. >> >> 6 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l6> >> >> # >> >> 7 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l7> >> >> # a = [] >> >> 8 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l8> >> >> # [1,2].product([4,5]) >> >> 9 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l9> >> >> # a #=> [[1, 4],[1, 5],[2, 4],[2, 5]] >> >> 10 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l10> >> >> # >> >> 11 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l11> >> >> # CREDIT: Thomas Hafner >> >> 12 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l12> >> >> >> >> 13 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l13> >> >> def product(*enums) >> >> 14 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l14> >> >> enums.unshift self >> >> 15 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l15> >> >> result = [[]] >> >> 16 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l16> >> >> while [] != enums >> >> 17 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l17> >> >> t, result = result, [] >> >> 18 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l18> >> >> b, *enums = enums >> >> 19 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l19> >> >> t.each do |a| >> >> 20 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l20> >> >> b.each do |n| >> >> 21 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l21> >> >> result << a + [n] >> >> 22 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l22> >> >> end >> >> 23 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l23> >> >> end >> >> 24 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l24> >> >> end >> >> 25 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l25> >> >> result >> >> 26 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l26> >> >> end >> >> 27 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l27> >> >> >> >> 28 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l28> >> >> end >> >> 29 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l29> >> >> >> >> 30 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l30> >> >> # Operator alias for cross-product. >> >> 31 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l31> >> >> # >> >> 32 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l32> >> >> # a = [1,2] ** [4,5] >> >> 33 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l33> >> >> # a #=> [[1, 4],[1, 5],[2, 4],[2, 5]] >> >> 34 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l34> >> >> # >> >> 35 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l35> >> >> # CREDIT: Trans >> >> 36 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l36> >> >> >> >> 37 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l37> >> >> alias_method :**, :product >> >> 38 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l38> >> >> >> >> 39 >> >> >> >> <http://facets.rubyforge.org/git?p=facets.git;a=blob;f=lib/core/facets/array/product.rb;h=f890ddc0ddd7e7f5076f207d0cc126b7bf34ceea;hb=HEAD#l39> >> >> end >> >> >> >> I'm guessing that wouldn't be too hard to port to PHP >> >> >> >> Cheers >> >> Julian Doherty >> >> >> >> > >> > >> > >> > > >> > >> >> > > > > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en -~----------~----~----~----~------~----~------~--~---
