-2

Ok, i have some assignment to do with PHP and i would appreciate some assistance. So, let's say i have multidimensional array like this one:

$testarray = array(0 => array(10, 20, 30),
                   1 => array(50, 60, 70),
                   2 => array(80, 90, 100),
                   .
                   .
                   .
                   n => array("", "", "",)

             );

Values in array are irrelevant, what matters are array keys. In this case, i have 3 keys in each array element, so when permutation is finished, final result should look like this:

[0] => Array (
              [0] => 1 1 1
              [1] => 2 1 1
              [2] => 3 1 1
              [3] => 1 2 1
              [4] => 2 2 1
              [5] => 3 2 1
              [6] => 1 3 1
              [7] => 2 3 1
              . 
              .
              .
              [n] => 3 3 3
              )

In case of 4 array keys, final result should look like this:

[0] = Array (
             [0] => 1 1 1 1
             [1] => 2 1 1 1
             [2] => 3 1 1 1
             [3] => 4 1 1 1
             [4] => 1 2 1 1
             .
             .
             .
             [n] => 4 4 4 4
            )

I would like to avoid recursion if possible.

Im having problems visualizing the whole looping process and initializing needed variables. I would really appreciate some help. Thank you.

Greedy
  • 115
  • 12
  • 1
    Are you **sure** you mean array keys? Because array keys are **unique** at their array level. – Epodax Mar 17 '16 at 09:59
  • 1
    What does your first array have to do with the permutations? The permutation formula is `p(a,b) = a!/(a-b)!` if that helps, but i'm not quite sure what you're asking? To clarify are you asking if given an `n` combination, where you they can be `x` different numbers -- generate all the permutations? – Mikey Mar 17 '16 at 10:00
  • Okay, let's not call it Permutations, i don't know how to name it, i just need some help with pointing me to right direction to output final result. – Greedy Mar 17 '16 at 10:03
  • I find it hard to understand what you are asking. – Epodax Mar 17 '16 at 10:07
  • 1
    I think he's asking if given a number, say `4`, build an array that contains all permutations of `p(4,4)`, if given `5` get all permutations of `p(5,5)` and so on. – Mikey Mar 17 '16 at 10:10
  • Yeah, something like that, does multiple foreach loop approach make any sense, giving the fact that that you dont know how many keys are in array ? – Greedy Mar 17 '16 at 10:13
  • If only the amount of keys matters, could do not do `count($array)` that way you have a number such as `6` and can use that as your boundaries per say? – Mikey Mar 17 '16 at 10:19
  • @Greedy Does the order matters? Like `1111` then `2111` or could it also be `1111` then `2222`, as long as you got all? – Rizier123 Mar 17 '16 at 10:21
  • If there is 3 keys in array, final order should be 111, 211, 311, then 121, 221, 321, and so on, until final 333. – Greedy Mar 17 '16 at 10:26
  • As far as i can see, he is using array values – Greedy Mar 17 '16 at 10:54

1 Answers1

2

I made some slight modifications to the original code in order to make it use the amount of keys instead of the array values, and then I added a second function to allow a multi-dimensional array to be counted as well.

<?php
function everyCombination($array) {
  $newArray = array();
  for($keyCount = 1; $keyCount <= count($array); $keyCount++){
    $newArray[] = $keyCount;
  }
  $arrayCount      = count($newArray);
  $maxCombinations = pow($arrayCount, $arrayCount);
  $returnArray     = array();
  $conversionArray = array();
  foreach ($newArray as $key => $value) {
    $conversionArray[base_convert($key, 10, $arrayCount)] = $value;
  }
  for ($i = 0; $i < $maxCombinations; $i++) {
    $combination = base_convert($i, 10, $arrayCount);
    $combination = str_pad($combination, $arrayCount, "0", STR_PAD_LEFT);
    $returnArray[] = strtr($combination, $conversionArray);
  }
  return $returnArray;
}

function getCombos($array){
    if(is_array($array[key($array)])){
        $return = array();
        foreach($array as $subArray){
            $return[] = everyCombination($subArray);
        }
    }else{
        $return = everyCombination($array);
    }
    return $return;
}


$test = array(53,22,1233,45);

echo '<pre>';
print_r(getCombos($test));
echo '</pre>';

All credit for function and usage goes to https://stackoverflow.com/a/14022357/2285345

Community
  • 1
  • 1
Epodax
  • 1,828
  • 4
  • 27
  • 32
  • I was actually asking for some directions and help with initializing variables, not to make people write code for me. Anyway, thank you for your efforts, lemme check that code. – Greedy Mar 17 '16 at 11:07
  • Doesn't print out as exactly as he wants it, but it's fairly close by the looks of it, should at least be a starting point for him to work off. – Mikey Mar 17 '16 at 11:08
  • @Greedy I figured, but it caught my interest and I wanted to solve it, at least to some degree. – Epodax Mar 17 '16 at 11:09