-1

I'm trying to accomplish the following but I can't seem to get it right. And even if I would, I don't think I'm doing it the right way.

Let's say I have this array:

$array = array(
    array(1, 2, 3),
    array(1, 2, 3),
    array(1, 2, 3)
);

Now I want to get all permutations of that multidimensional array, but every 'subarray' must still have a 1, 2 and 3. So these are ok:

$array = array(
    array(2, 1, 3),
    array(1, 2, 3),
    array(1, 2, 3)
);

$array = array(
    array(2, 3, 1),
    array(1, 2, 3),
    array(1, 2, 3)
);

But this is not:

$array = array(
    array(3, 3, 3),
    array(1, 2, 1),
    array(1, 2, 2)
);

I've tried quite a bit, but all that I keep getting back to is a MASSIVE amount of for-loops. And that doesn't seem like the right way to accomplish this.

I hope someone can help!

RGweb
  • 109
  • 3
  • 8
  • Do you search unique permutations, or just random once? And where is YOUR code? – djot Sep 21 '13 at 17:31
  • This might give you a kick-start: http://stackoverflow.com/a/5506933/1057429 – Nir Alfasi Sep 21 '13 at 17:36
  • http://php.net/shuffle – BlitZ Sep 21 '13 at 17:40
  • @alfasin I've indeed seen that one, and it was great for getting all permutations of an 2d array, but I need permutations for a multidimensional array. – RGweb Sep 21 '13 at 17:41
  • @djot I need all permutations that are compliant with the rules I specified before. And I didn't add my code here, because I'm afraid that would make people only try to change little things in my code, and not try out a better solution. – RGweb Sep 21 '13 at 17:43
  • 1
    Well, ALL or not ALL ... your examples are incorrect then, the second array in the second block 1,2,3 - 1,2,3 is the same and the array does NOT contain ALL variants. – djot Sep 21 '13 at 17:47
  • @RGweb you should do some work by yourself - it's a long shot that someone here will put the effort of doing it for you. Use the permutations code to get all permutations then traverse them to output the required result which is, by the way, not clear to (at least) djot & me. – Nir Alfasi Sep 21 '13 at 20:09
  • @djot There's nothing wrong with the examples. Let's look at it as a grid with rows and columns. All rows must have 1, 2 and 3. And can NOT have double numbers. So 1, 1, 2 or 3, 3, 3 is incorrect. But rows don't have to be unique, two rows with 1, 2, 3 ar totally fine. – RGweb Sep 22 '13 at 08:58
  • So just use `shuffle()` and you're done. – djot Sep 22 '13 at 11:12
  • @djot But `shuffle()` us truly random, and I'm not asking for random permutations, I'm asking for ALL permutations that are complaint to the above rules. If I do shuffle 1000 times, it could be that of those 1000 a few are the same. – RGweb Sep 22 '13 at 11:24

2 Answers2

-1

I don't get what you want - and you cannot express clearly what you want ... but this creates at least what you have given as examples.

$array = array(
  array(1, 2, 3),
  array(1, 2, 3),
  array(1, 2, 3)
);


foreach ($array AS $key => $values) {
  shuffle($array[$key];
}

var_dump($array);
djot
  • 2,952
  • 4
  • 19
  • 28
-2

Code is a modified version of: Get all permutations of a PHP array?

If your set always has three distinct values, you can do this with three nested for loops. Otherwise, if you need to this on variable sized inputs, you will need to perform recursion. Also, if the sub arrays always contain the same values, it needn't be multi dimensional. Something like this:

function getPerms($items, $perms = array(), &$return) {
    if (empty($items)) {
      $return[] = $perms;
    }
    else {
      for ($i = count($items) - 1; $i >= 0; --$i) {
         $newitems = $items;
         $newperms = $perms;
         list($foo) = array_splice($newitems, $i, 1);
         array_unshift($newperms, $foo);
         $this->pc_permute($newitems, $newperms, $return);
      }
    }
 }

then call it with:

$return = array();
getPerms(array(1,2,3), array(), $return);

$return will contain all the permutations of those three numbers.

Community
  • 1
  • 1
Zack Newsham
  • 2,810
  • 1
  • 23
  • 43
  • A) When you copy code from another place you should credit the writer. And B) I already provided a link to this code in the comments above and the OP stated that it doesn't solve his problem. – Nir Alfasi Sep 21 '13 at 20:06
  • A) You are correct - I had intended to put that at the bottom that I'd used a modified version of code I'd found, but B) I was under the impression that it failed to satisfy because it didnt return an array of all the possible permutations, rather it just echo'd them out as it received them. Additionally, I addressed the issue that his question doesnt require a multiple dimension array. Incidentally - I didnt get the code from your link, I got it from here. http://stackoverflow.com/questions/10222835/get-all-combinations-of-a-php-array – Zack Newsham Sep 21 '13 at 20:20