1

i'm trying to output all the possible combinations of 3 elements in array, output should echo 3 elements at a time. I know the formula to know the number of possibilities ( n! / (n-k)! )

Currently this produces the output i want for an array of size 3

php code

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

for ($i=0;$i<count($items);$i++) {
    $pos = $items[$i];
    $rest = $items;
    unset($rest[$i]);
    $rest = array_values($rest);
    echo $pos.$rest[0].$rest[1]."<br/>";
    echo $pos.$rest[1].$rest[0]."<br/>";
}

output:

ABC ACB BAC BCA CAB CBA

I wanted my function to be dynamic and more scalable, to take in 2 or more elements and be able to permute them all without repetition, been scratching my head trying to figure this out.

brunobliss
  • 364
  • 1
  • 16

0 Answers0