Re: Too many arrays! My head is exploding!

From: Date: Tue, 29 May 2012 14:48:54 +0000
Subject: Re: Too many arrays! My head is exploding!
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-318018@lists.php.net to get a copy of this message
Florian Lemaitre wrote: > Le 29/05/2012 14:45, Gary a écrit : >> Okay, let's assume I have three "things", A, B, and C. I need to > produce >> an array with a list of all possible combinations of them, however > many >> there might be in those combinations: e.g. A, B, C, D, AB, AC, AD, BC, >> ABC (not sure if I've missed any!). > Enjoy ! > > function combinations($letters) { > $combinations = array(array()); > foreach ($letters as $letter) { > foreach ($combinations as $combination) { > $combinations[] = array_merge($combination, array($letter)); > } > } > return $combinations; > } :-) Looks to do pretty much exactly what I want, thanks! The key is the array_merge, I guess :) Let's just say I wrote a *lot* more code than that :") -- Gary Please do NOT send me 'courtesy' replies off-list.

« previous php.general (#318018) next »