Logic of all possible mixtures of values of a variable quantity array. PHP

0

I'm breaking my head to create a logic that does the following:

Get an array of N positions, within these positions define all possible possibilities without repeating the key of a position.

Example: Array ('A', 'B', 'C')

A AB ABC B.C ACB
B BA BAC BC BCA
W HERE CAB CB CBA

Examples of the mathematical formula

When there are 3 positions:
3+ (3 * 2) + (3 * 2 * 1)
When there are 4 positions:
4 + (4 * 3) + (4 * 3 * 2) + (4 * 3 * 2 * 1)     

asked by anonymous 25.08.2015 / 15:28

1 answer

1

If there is control over the number of variables that come in, you can create X structures for , where X is the number of variables of array , doing something like:

$arr = array("A","B","C");

for ($i = 0; $i < count($arr); $i++) {
    echo $arr[$i]."\n";
    for ($j = 0; $j < count($arr); $j++) {
        if ($i != $j) {
            echo $arr[$i].$arr[$j]."\n";
            for ($k = 0; $k < count($arr); $k++) {
                if ($i != $j && $i != $k && $j != $k) {
                    echo $arr[$i].$arr[$j].$arr[$k]."\n";
                }
            }
        }
    }
}

See working at ideone

It is a more manual process, and would require a greater amount of coding, I will see if I can create something more automatic.

    
25.08.2015 / 15:48