Re: Too many arrays! My head is exploding!
| From: | Adam Richardson | Date: | Tue, 29 May 2012 15:41:40 +0000 |
| Subject: | Re: Too many arrays! My head is exploding! | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-318020@lists.php.net to get a copy of this message | ||
On Tue, May 29, 2012 at 10:55 AM, Tedd Sperling <tedd@sperling.com> wrote:
> On 29 May 2012 18:15, Gary <listgj-phpgeneral@yahoo.co.uk> wrote:
>
>> 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!). Normally I'm pretty good at working
>> this stuff out, but to be honest I'm struggling with this one, at least
>> to do it in any kind of elegant way. Does anyone have any ideas?
>
> Sure, but what you are asking for is a permutation and not a combination.
His example seems to suggest order does not matter (I've omitted 'D',
which I suspect was a typo given the set of A, B, and C):
A
B
C
AB
AC
BC
ABC
If order did matter, he would have included BA, etc.
That all said, combinations typically involve a consistent number of
choices, and his example includes various ranges of r.
These would be combinations for r = 1:
A
B
C
These would be combinations for r = 2:
AB
AC
BC
This would be the combination for r = 3:
ABC
What it seems like he's after is the power set of set ABC (minus the empty set):
http://en.wikipedia.org/wiki/Power_set
Adam
--
Nephtali: A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com