2

I have the following problem:

$multidmimensional = array(

 [0] => array(
    [0] => 1, 
    [1] => 2, 
    [2] => 3
  );


  [1] => array(
   [0] => 5, 
   [1] => 6, 
   [2] => 7
  );

  ...

  [2] => array(
   [0] =>,4 
   [1] => 5, 
  );

);

I can have one or more (nested) arrays, and lets take as an example the first two of the above arrays:

I should permutate them in the following way:

15
16
17

25
26
27

36
37
38

If I had for example those three arrays, I should get a result like this:

154
164
174
155
165
175

254
264
274
255
265
275

364
374
384
365
375
385

I am having some problems to make an algorithm that would fix this problem. Can anyone help me? Thanks in advance.

3
  • Can we see the code you have thus far? Also, if you would format your code with indentation and <pre> tags, that would make it easier to read. Commented May 24, 2012 at 9:17
  • Please, learn to Markdown. No need for tons of <br> and unreadable code. Commented May 24, 2012 at 9:20
  • Where is the problem that needs to be fixed? Commented May 24, 2012 at 10:02

3 Answers 3

3

That's a nice brain teasing question. Here's what I came up with, see the running demo for testing and adjusting.

$multidimensional = array(
  0 => array(
    0 => 1,
    1 => 2,
    2 => 3,
  ),
  1 => array(
    0 => 5,
    1 => 6,
    2 => 7,
  ),
  2 => array(
    0 => 4,
    1 => 5,
  ),
); // just your input


$permutations = array();
$count = count($multidimensional);
for ($i = 0; $i < $count; $i++) {
  $temp = array_map("permute",array($permutations),array($multidimensional[$i]));
  $permutations = $temp[0];
}
print_r($permutations); // OUTPUT

function permute($base,$add) {
  $result = array();
  if (count($base) > 0) {
    foreach ($base AS $val1) {
      if (count($add) > 0) {
        foreach ($add AS $val2) {
          $result[] = $val1.$val2;
        }
      }
      else {
        $result = $base;
      }
    }
  }
  else {
    $result = $add;
  }
  return $result;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I can not test it right now but this should work: (may contain typos)

function permute($arrays){
      if(count($arrays)<2) return $arrays[0];//TODO error on count == 0
      $array1 = array_shift($arrays);
      $array2 = array_shift($arrays);
      $results = array();
      foreach($array1 as $elementOfOne){
        foreach($array2 as $elementOfTwo){
          $results[] = $elemnetOfOne . $elementOfTwo;
        }
      }
      array_unshift($arrays, $results);
      return permute($arrays);
    }

Comments

0
$data = array
(
  '1' => array(5, 6, 7),
  '2' => array(9, 25, 14)

);

for($i=0; $i<=count(array_keys($data)); $i++) {
   for($j=1; $j<=2; $j++) {
      $values[$i][] = $data[$j][$i];
   }
}

for($i=0; $i<count($values); $i++) {
  shuffle($values[$i]);
}

$newData = array();
for($i=0; $i<3; $i++) {
   for($j=1; $j<=2; $j++) {
       $newData[$j][] = array_pop($values[$i]);
   }
}
print_r($newData);

Fiddle

1 Comment

this works only for the array exactly as given, so much as changing the index will break it.

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.