Re: Too many arrays! My head is exploding!

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 29 May 2012 18:15, Gary <listgj-phpgeneral@xxxxxxxxxxx> 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?
>
> Idealy what I'd like is a multidimensional array depending on the number
> of "things" in the combination. Something like:
> array(2) {
>  [0]=>
>  array(3) {
>    [0]=>
>    array(1) {
>      ["name"]=>
>      string(7) "A"
>    }
>    [1]=>
>    array(2) {
>      ["name"]=>
>      string(5) "B"
>    }
>    [2]=>
>    array(2) {
>      ["name"]=>
>      string(4) "C"
>    }
>  }
>  [1]=>
>  array(...) {
>    [0]=>
>    array(2) {
>      ["name"]=>
>      string(13) "A+B"
>    }
>    [1]=>
>    array(2) {
>      ["name"]=>
>      string(12) "A+C"
>    }
> (etc.)
>
>
>

If you are using linux, you can use the following:

$a = shell_exec("echo {a,b,c,d} && echo {a,b,c,d}{a,b,c,d} && echo
{a,b,c,d}{a,b,c,d}{a,b,c,d} && echo {a,b,c,d}{a,b,c,d}{a,b,c,d}{a,b,c,d}");

This will give you one line for 1 character combination, 1 line for 2
character combination and so on. You can then use explode to convert each
line to arrays.

Thanks,
Vikash




> --
> Gary        Please do NOT send me 'courtesy' replies off-list.
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux