5

Possible Duplicate:
Permutations - all possible sets of numbers

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

I want to build a probability tree using these options:

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

So one valid line could be 1-2-E-3-O-I-4-A

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

Community
  • 1
  • 1
Hailwood
  • 89,623
  • 107
  • 270
  • 423

2 Answers2

0

Recursion is probably the easiest way to implement this, but it won't scale well to large datasets.

Basically write a function that takes an array of options, chops one off an calls itself.

troelskn
  • 115,121
  • 27
  • 131
  • 155
0
<?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 );

?>
Peon
  • 7,902
  • 7
  • 59
  • 100