Build a probability tree with php?

Possible duplicate:
Permutations - all possible sets of numbers

I have an array that has a list of options. Each parameter is unique and cannot be repeated.

I want to build a probability tree using the following parameters:

$options = array('1','2','3','4','A','E','I','O');

So a valid string could be 1-2-E-3-O-I-4-A

How can i do this? (or at least point me in the right direction!)

+5
source share
2 answers
<?php

function pc_permute($items, $perms = array( )) {
    if (empty($items)) { 
        print join('-', $perms) . "<br />";
    }  else {
        for ($i = count($items) - 1; $i >= 0; --$i) {
            $newitems = $items;
            $newperms = $perms;
            list($foo) = array_splice($newitems, $i, 1);
            array_unshift($newperms, $foo);
            pc_permute($newitems, $newperms);
        }
    }
}

$options = array( '1','2','3','4','A','E','I','O' );
$mass = pc_permute( $options );

?>
0
source

Recursion is probably the easiest way to implement this, but it will not scale well for large datasets.

In principle, writing a function that takes an array of parameters discards one of the calls.

0
source

All Articles