2

I'm trying to build a php script that, for a given number of cats and collars, builds an array of all possible permutations.

For example:

I have 4 different collars: A, B, C and D. If I have 1 cat, I have 4 possible permutations - they can be wearing collar A, B, C or D.

It turns out that the number of possible permutations is:

4n
Thus if I have 2 cats I have 16 possible permutations, 3 cats 64 permutations etc.

So, if I have the following code:

$collars = array('yellow','blue','green','pink');
$cats = array('bob','frank');
$no_cats = count($cats);

How can I get it to generate an array of these permutations?

Array
(
  [0] => Array
    (
      [bob] => yellow,
      [frank] => yellow
    )
  [1] => Array
   (
      [bob] => yellow,
      [frank] => blue
   )
  
 [...]
  
  [15] => Array
   (
      [bob] => pink,
      [frank] => pink
   )
 )

Here's where I've got to (I know it's wrong not least because the number of collar loops is hardcoded to the number of cats):

$collars = array('yellow', 'blue', 'green', 'pink');
$cats = array('bob', 'frank');
$i = 0;

foreach ($collars as $collar_a){
    foreach ($collars as $collar_b){
        foreach ($cats as $cat){
            $output[$i][$cat] = $collar_a;
            $output[$i][$cat] = $collar_b;
        }
        $i++;
    }
}

print_r($output);

This generates:


Array
(
    [0] => Array
        (
            [bob] => yellow
            [frank] => yellow
        )

    [1] => Array
        (
            [bob] => blue
            [frank] => blue
        )

    [2] => Array
        (
            [bob] => green
            [frank] => green
        )

    [3] => Array
        (
            [bob] => pink
            [frank] => pink
        )

    [4] => Array
        (
            [bob] => yellow
            [frank] => yellow
        )

    [5] => Array
        (
            [bob] => blue
            [frank] => blue
        )

    [6] => Array
        (
            [bob] => green
            [frank] => green
        )

    [7] => Array
        (
            [bob] => pink
            [frank] => pink
        )

    [8] => Array
        (
            [bob] => yellow
            [frank] => yellow
        )

    [9] => Array
        (
            [bob] => blue
            [frank] => blue
        )

    [10] => Array
        (
            [bob] => green
            [frank] => green
        )

    [11] => Array
        (
            [bob] => pink
            [frank] => pink
        )

    [12] => Array
        (
            [bob] => yellow
            [frank] => yellow
        )

    [13] => Array
        (
            [bob] => blue
            [frank] => blue
        )

    [14] => Array
        (
            [bob] => green
            [frank] => green
        )

    [15] => Array
        (
            [bob] => pink
            [frank] => pink
        )

)

It has the right number of loops but clearly not the right permutations. I suspect this can easily be solved by a self-referencing function but they're above my skill-level.

Any help would be enormously appreciated as I've been pulling my hair out over this for 2 days now!

1 Answer 1

1

You will require 3 nested loops for this.

  • First outer loop to loop over all cats.
  • Second outer loop to loop over all previously collected combination results for previous cats.
  • Third inner loop to add current cat and collar combination to previously collected result.

The code below I believe is readable accordingly.

<?php

$collars = array('yellow','blue','green','pink');
$cats = array('bob','frank');

$result = [[]];
foreach($cats as $cat){
    $temp = [];
    foreach($result as $r){
        foreach($collars as $collar){
            $temp[] = array_merge($r, [ $cat => $collar]); 
        }
    }
    $result = $temp;
}


print_r($result);

Total combinations possible is not 4n all the time. Say, size of cats is m and size of collars is n. So, total combinations possible is nm.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.